[英]How to perform .Select Linq Operation in a ObservableCollection<dynamic> in C#
我有一個ObservableCollection<dynamic>
它包含一個ObservableCollection<MobileModel>
。 現在,我需要從ObservableCollection<dynamic>
選擇“ 品牌屬性”
類文件是
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);
SelectBrand(new ObservableCollection<dynamic>(MobileList.Cast<dynamic>()), "Brand");
}
public void SelectBrand(ObservableCollection<dynamic> Source, string propertyName)
{
// How to Create perform the Select Operation for the Property "Brand" as specified in the second parameter "propertyName"
List<string> BrandList = new List<string>();
}
}
public class MobileModel : Notify
{
private string _brand = string.Empty;
private ObservableCollection<MobileModelInfo> _model = new ObservableCollection<MobileModelInfo>();
private string _os = string.Empty;
public string Brand
{
get { return _brand; }
set { _brand = value; OnPropertyChanged(); }
}
public ObservableCollection<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; }
}
我在這里不了解dynamic
的用法。 據我了解的問題,除了指定屬性名稱而不是執行選擇器委托之外,您希望能夠實質上實現LINQ的Select()
。 由於屬性名稱在編譯時未知,因此我看不到dynamic
會有什么幫助。
因此,暫時忽略dynamic
,您要問的是使用反射很簡單。 例如:
class A
{
public string P { get; set; }
}
class Program
{
static void Main(string[] args)
{
A[] rga =
{
new A { P = "One" },
new A { P = "Two" },
new A { P = "Three" },
};
foreach (string value in SelectProperty(rga, "P"))
{
Console.WriteLine(value);
}
}
private static IEnumerable<string> SelectProperty<T>(IEnumerable<T> rga, string propertyName)
{
PropertyInfo pi = typeof(T).GetProperty(propertyName);
foreach (T t in rga)
{
yield return (string)pi.GetValue(t);
}
}
}
請注意,在上面,因為我使用的是實際的元素類型而不是dynamic
,所以不必將我的集合包裝在該集合的全新副本中,並且可以直接訪問該類型的反射數據(即PropertyInfo
)通過編譯器語法,而不必深入研究實際傳遞給我的集合對象。
您應該能夠在代碼中使用類似的東西,並且可以通過直接傳遞MobileList
對象引用來調用它。 例如:
List<string> brandList = SelectProperty(MobileList, "Brand").ToList();
對我來說似乎好多了。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.