【问题标题】:Using LINQ to loop through inner class properties in outer class collection使用 LINQ 循环遍历外部类集合中的内部类属性
【发布时间】:2014-01-14 08:37:12
【问题描述】:

利用问答处理循环遍历对象的属性(https://stackoverflow.com/questions/15586123/loop-through-object-and-get-properties、Loop Through An Objects Properties In C#),您可以:

  • Class1 对象的集合(即 listObj1)
  • 每个 Class1 包含 Class2 对象的集合(即 dictObj2)

你会怎么做:

  • 高效判断内部类(Class2)的属性
  • 循环遍历内部类(Class2)的属性
  • 遍历 Class1 对象 (listObj1) 的集合,选择 Class2 属性的所有实例
  • 输出 Class2 属性的集合(例如,第一次迭代将返回 MeasurementA 的集合,每个 Class1 对象都有一个)。
  • 并按 Class1.PropertyA 和 Class1.PropertyB 对集合进行分组

请在下面找到所涉及类的粗略地图。

我一直在尝试使用 LINQ 查询但没有成功。任何想法或指导将不胜感激。

class MainClass {
  List<Class1> listObj1
}

class Class1 {
  // a number of fields including...
  int PropertyA { get; set; }
  int PropertyB { get; set; }
  Dictionary<int, Class2> dictObj2 { get; set; }
}

class Class2 {
  // a number of fields all of type double...
  double MeasurementA { get; set; }
  double MeasurementB { get; set; }
  double MeasurementC { get; set; }
}

【问题讨论】:

  • “有效地确定内部类的属性”——通过使用interface。
  • 你的 LINQ 查询是什么样的?
  • 你不知道这些类的类型吗?如果不是,那么您怎么知道外部类将具有内部类的集合?听起来您在问如何使用反射,但如果您提前知道类型,反射通常应该是不必要的。
  • @jmcilhinney Class1 将始终包含多个 Class2 对象。所有 Class2 属性将始终存在。你是对的,类型总是提前知道的。我同意反射不应该是必要的,但我不知道另一种方法来做到这一点。欢迎提出任何建议。

标签: c# linq


【解决方案1】:

给定数据:

MainClass mainClass = new MainClass();
mainClass.listObj1 = new List<Class1>()
{
    new Class1() { 
        dictObj2 = new Dictionary<int,Class2>() {
            { 1, new Class2() { MeasurementA = 2.0, MeasurementB = 3.0, MeasurementC = 4.0 }},
            { 2, new Class2() { MeasurementA = 5.0, MeasurementB = 6.0, MeasurementC = 7.0 }}
        }
    }
};

您可以使用 LINQ 编写代码:

var fields = typeof(Class2)
             // Get Properties of the ClassB
             .GetProperties(BindingFlags.Public | BindingFlags.Instance)
             // Map each PropertyInfo into collection of its values from c1.dictObj2
             .SelectMany(pi => mainClass.listObj1
                                        .SelectMany(c1 => c1.dictObj2)
                                        .Select(p => new
                                                 {
                                                   Property = pi.Name,
                                                   Value = pi.GetValue(p.Value)
                                                 }))
             // Group data with respect to the PropertyName
             .GroupBy(x => x.Property, x => x.Value)
             // And create proper dictionary
             .ToDictionary(x => x.Key, x => x.ToList());

现在您有一个Dictionary,其键为ClassB 属性名称和值作为这些属性值的List。

【讨论】:

  • 感谢您的回复。如果返回的字典还需要 CompositeKey(基于 Class2 属性名称和 Class 1 属性的键,例如 Struct(c1.PropA, c1.PropB, dict.PropName)),如何检索?
【解决方案2】:

高效确定内部类(Class 2)的属性

不管效率如何,实际上只有一种方法可以做到(假设您的意思是在运行时),那就是使用反射。

循环遍历内部类(Class 2)的属性

foreach (var prop in instance.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
    Console.WriteLine(prop.Name);
}

遍历 Class1 对象 (listObj1) 的集合,选择 Class2 属性的所有实例

foreach (var obj in mainClass.listObj1)
{
    var innerClasses = obj.dictObj2.Values;
    // do something with inner classes
}

输出 Class2 属性的集合(例如,第一次迭代将返回 MeasurementA 的集合,每个 Class1 对象都有一个)。

foreach (var prop in typeof(Class2).GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
    foreach (var obj in mainClass.listObj1)
    {
        foreach (var innerClass in obj.dictObj2.Values)
        {
            Console.WriteLine(prop.GetValue(innerClass, null));
        }
    }   
}

不知道你为什么如此热衷于在这里使用 LINQ,我认为这里只需要几个简单的 for 循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多