如何在C#中将LinqSelectEnumerable转换为首选类型

本文关键字:类型 转换 LinqSelectEnumerable 中将 | 更新日期: 2023-09-27 18:28:31

我有一个ObservableCollection,即MobileModel类型的MobileList。MobileModel有一个MobileModelInfo列表。

我只需要MobileList中的MobileModelInfo。所以我在方法ExtractFirstMobileList()中使用了LinqSelect语句。但我无法分配列表,它显示了一个错误。

我的C#源代码:

public class Mobile
{
    private ObservableCollection<MobileModel> _mobileList;
    public ObservableCollection<MobileModel> MobileList
    {
        get { return _mobileList; }
        set { _mobileList = value; OnPropertyChanged(); }
    }
    public void GetMobile()
    {
        List<MobileModel> mList = new List<MobileModel>();
        List<MobileModelInfo> modList = new List<MobileModelInfo>();
        MobileModel mob = new MobileModel();
        modList.Clear();
        mob.Brand = "Apple";
        modList.Add(new MobileModelInfo { Name = "iPhone 4", Catagory = "Smart Phone", Year = "2011" });
        modList.Add(new MobileModelInfo { Name = "iPhone 5", Catagory = "Smart Phone", Year = "2013" });
        modList.Add(new MobileModelInfo { Name = "iPhone 6", Catagory = "Premium Smart Phone", Year = "2015" });
        mob.Model = new ObservableCollection<MobileModelInfo>(modList);
        mob.OS = "IOS";
        mList.Add(mob);
        mob = new MobileModel();
        modList.Clear();
        mob.Brand = "Samsung";
        modList.Add(new MobileModelInfo { Name = "S4", Catagory = "Smart Phone", Year = "2011" });
        modList.Add(new MobileModelInfo { Name = "S5", Catagory = "Smart Phone", Year = "2013" });
        modList.Add(new MobileModelInfo { Name = "S6", Catagory = "Ultra Smart Phone", Year = "2015" });
        mob.Model = new ObservableCollection<MobileModelInfo>(modList);
        mob.OS = "Android";
        mList.Add(mob);
        mob = new MobileModel();
        modList.Clear();
        mob.Brand = "MicroSoft";
        modList.Add(new MobileModelInfo { Name = "Lumina 9900", Catagory = "Phone", Year = "2011" });
        modList.Add(new MobileModelInfo { Name = "Opera X220", Catagory = "Smart Phone", Year = "2013" });
        mob.Model = new ObservableCollection<MobileModelInfo>(modList);
        mob.OS = "Windows";
        mList.Add(mob);
        mob = new MobileModel();
        modList.Clear();
        mob.Brand = "Sony Ericssion";
        modList.Add(new MobileModelInfo { Name = "S4", Catagory = "Smart Phone", Year = "2011" });
        modList.Add(new MobileModelInfo { Name = "S5", Catagory = "Smart Phone", Year = "2013" });
        modList.Add(new MobileModelInfo { Name = "S6", Catagory = "Ultra Smart Phone", Year = "2015" });
        mob.Model = new ObservableCollection<MobileModelInfo>(modList);
        mob.OS = "Android";
        mList.Add(mob);
        MobileList = new ObservableCollection<MobileModel>(mList);
        ExtractFirstMobileList(MobileList);
    }

    public void ExtractFirstMobileList(ObservableCollection<MobileModel> Source)
    {
        List<MobileModelInfo> mInfo = Source.Select(t=> t.Model);
    }
}
public class MobileModel : Notify
{
    private string _brand = string.Empty;
    private List<MobileModelInfo> _model = new List<MobileModelInfo>();
    private string _os = string.Empty;
    public string Brand
    {
        get { return _brand; }
        set { _brand = value; OnPropertyChanged(); }
    }
    public List<MobileModelInfo> Model
    {
        get { return _model; }
        set { _model = value; OnPropertyChanged(); }
    }
    public string OS
    {
        get { return _os; }
        set { _os = value; OnPropertyChanged(); }
    }
}
public class MobileModelInfo
{
    public string Name { get; set; }
    public string Catagory { get; set; }
    public string Year { get; set; }
}

请帮助我如何将Enumerable强制转换为首选类型。

如何在C#中将LinqSelectEnumerable转换为首选类型

如果您想创建一个包含所有不同MobileModelInfo的扁平列表,可以使用SelectMany:

List<MobileModelInfo> mInfo = Source.SelectMany(t => t.Model).ToList();

但是您的方法被称为ExtractFirstMobileList,这表明您想要每个子列表中的第一个项目——在这种情况下,您可以使用:

List<MobileModelInfo> mInfo = Source.Select(t => t.Model.First()).ToList();

希望这能帮助

MobileModel类上的Model属性本身就是一个列表。因此,您使用的选择查询将返回MobileModelInfo集合的集合(返回类型IEnumerable<List<MobileModelInfo>>)

要将其转换为列表<列表<MobileModelInfo>>在末尾添加一个.ToList()。

List<List<MobileModelInfo>> mInfo = Source.Select(t => t.Model).ToList();

如果需要使用LINQ导出列表,请在使用Select()方法之后使用ToList()方法。还要确保您的列表不是空的,它存在并且包含0个以上的元素