如何执行。在C#中的ObservableCollection <dynamic>中选择Linq操作

时间:2015-12-04 07:04:14

标签: c# linq dynamic observablecollection

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

1 个答案:

答案 0 :(得分:2)

我不明白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对象引用来调用它。 E.g:

List<string> brandList = SelectProperty(MobileList, "Brand").ToList();

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