如何执行.选择ObservableCollection中的Linq操作<;动态>;在C#中

本文关键字:动态 lt gt 操作 Linq 何执行 执行 中的 ObservableCollection 选择 | 更新日期: 2023-09-27 18:25:40

我有一个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; }
}

如何执行.选择ObservableCollection中的Linq操作<;动态>;在C#中

我不理解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();

这对我来说似乎好多了。:)