![](/img/trans.png)
[英]C# Cast object to IEnumerable<T> and use Linq extensions when the enumerable type is a value type e.g. enum
[英]How to Cast Linq Select Enumerable to preferred type in C#?
我有一個ObservableCollection
即MobileModel
類型的MobileList
。 MobileModel具有MobileModelInfo
列表。
我只需要該MobileList
MobileModelInfo
。 所以我在方法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();
希望這可以幫助
MobileModel類的Model屬性本身就是一個列表。 因此,您使用的選擇查詢將返回MobileModelInfo的集合的集合(返回類型IEnumerable <List <MobileModelInfo >>)
要將其轉換為List <List <MobileModelInfo >>,請在末尾添加.ToList()。
List<List<MobileModelInfo>> mInfo = Source.Select(t => t.Model).ToList();
如果需要使用LINQ
壓縮列表,請在Select()
方法之后使用ToList()
Select()
方法。 另外,請確保您的列表不為空,它存在並且包含超過0個元素
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.