2015-12-30 41 views
1

我有ObservableCollection,即MobileList類型MobileModel。 MobileModel有一個列表MobileModelInfo如何投射Linq選擇Enumerable爲C#中的首選類型?

我只需要MobileModelInfoMobileList。所以我在方法ExtractFirstMobileList()中使用了Linq Select Statement。但我無法分配列表,它顯示一個錯誤。

我的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; } 
} 

請幫助我如何可枚舉轉換爲首選類型。

+0

什麼是錯誤? – Viru

回答

5

如果你想創建一個所有不同MobileModelInfo扁平列表,你可以使用SelectMany

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

但你的方法被稱爲ExtractFirstMobileList,這表明希望在每個子列表中的第一個項目 - 在這種情況下,你可以使用:

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

希望這有助於

3

MobileModel類的Model屬性本身就是一個列表。所以,你使用SELECT查詢將返回MobileModelInfo(返回類型的IEnumerable <名單< MobileModelInfo >>)的集合的集合

將其轉換爲以列表<名單< MobileModelInfo >>添加.ToList()結束。

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

如果需要extreact列表以及後ToList()方法Select()方法LINQ使用。另外要確保你的列表不是空的,它的存在和包含超過0元素