c# - 如何在C#中将Linq Select Enumerable转换为首选类型?

标签 c# linq select casting

我有一个ObservableCollectionMobileList类型的MobileModel。 MobileModel的列表为MobileModelInfo

我只需要该MobileModelInfo中的MobileList。因此,我在方法ExtractFirstMobileList()中使用了Linq Select语句。但是我无法分配列表,它显示了一个错误。

我的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转换为首选类型。

最佳答案

如果要使用所有不同的MobileModelInfo创建扁平化列表,则可以使用SelectMany

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


但是您的方法称为ExtractFirstMobileList,它建议您要每个子列表中的第一项-在这种情况下,您可以使用:

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


希望这可以帮助

关于c# - 如何在C#中将Linq Select Enumerable转换为首选类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34524241/

相关文章:

c# winForms 输出到文本文件

c# - 你会如何限制每秒的操作次数?

c# - 按字段排序,该字段是 NHibernate/LINQ 中其他两个字段的组合/函数

c# - 这两个linq查询之间有什么区别?

mysql - 如何在mySql中多次执行select语句?

Mysql:选择任意年份 1 月 22 日至 2 月 3 日之间的时间戳

c# - 如何从 C# 中的文本文件中删除一行?

c# - 使用 Lambda/Linq 获取按字母顺序排列的项目列表?

mysql - 如何使用 MySQL 选择有 2 个具有特定值的连续行?

c# - 新创建的MVC的_LogOnPartial文件