当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《如何在C#中将LinqSelectEnumerable转换为首选类型?》经验,为你挑选了1个好方法。

我有一个ObservableCollection就是MobileList类型MobileModel.MobileModel有一个List of MobileModelInfo.

我只需要MobileModelInfo那个MobileList.所以我在方法中使用了Linq Select Statement ExtractFirstMobileList().但是我无法分配List,它显示错误.

我的C#源代码:

public class Mobile
{
    private ObservableCollection _mobileList;
    public ObservableCollection MobileList
    {
        get { return _mobileList; }
        set { _mobileList = value; OnPropertyChanged(); }
    }

    public void GetMobile()
    {
        List mList = new List();
        List modList = new List();
        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(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(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(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(modList);
        mob.OS = "Android";
        mList.Add(mob);

        MobileList = new ObservableCollection(mList);

        ExtractFirstMobileList(MobileList);

    }


    public void ExtractFirstMobileList(ObservableCollection Source)
    {
        List mInfo = Source.Select(t=> t.Model);
    }

}

public class MobileModel : Notify
{
    private string _brand = string.Empty;
    private List _model = new List();
    private string _os = string.Empty;

    public string Brand
    {
        get { return _brand; }
        set { _brand = value; OnPropertyChanged(); }
    }
    public List 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转换为首选类型.



1> Zrethreal..:

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

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

但是调用了你的方法ExtractFirstMobileList,这表明你想要每个子列表中的第一个项目 - 在这种情况下你可以使用:

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

希望这可以帮助

推荐阅读
拾味湖
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有