首页 > 解决方案 > C# LINQ:从外部类集合中总结特定的内部类属性

问题描述

在 C# 中循环遍历对象属性并使用LINQ循环遍历外部类集合中的内部类属性

如果您在集合(PhaseRepo)中有对象(Phase),我相信可以在对象(Phase)中指定 propertiesOfInterest 并创建一个 Dictionary 来总结属性。

请在下面找到我在 LinqPad 中的尝试。请协助语法或建议替代方法。

谢谢

enum Dir {Up, Dn}

struct BmkKey
{
    public Dir Direction;
    public string DetailType;
}

class Phase
{
    public Dir Direction { get; set; }
    public double StartToTerminationBars { get; set; }
    public double StartToTerminationPriceChange { get; set; }
    public double StartToTerminationGa { get; set; }
    public double NotRequiredProperty { get; set; }
}

class PhaseRepo
{
    public List<Phase> Phases { get; private set; }

    public List<Phase> GetPhases()
    {
        return new List<Phase>()
        { 
            new Phase() { Direction = Dir.Up, StartToTerminationBars = 3.0, StartToTerminationPriceChange = 4.0, StartToTerminationGa = 4.0},
            new Phase() { Direction = Dir.Up, StartToTerminationBars = 6.0, StartToTerminationPriceChange = 8.0, StartToTerminationGa = 4.0},
            new Phase() { Direction = Dir.Dn, StartToTerminationBars = 3.0, StartToTerminationPriceChange = -4.0, StartToTerminationGa = -4.0},
            new Phase() { Direction = Dir.Dn, StartToTerminationBars = 6.0, StartToTerminationPriceChange = -8.0, StartToTerminationGa = -4.0},
        };
    }
}

void Main()
{
    var phaseRepo = new PhaseRepo();
    var phases = phaseRepo.GetPhases();
    //phases.Dump();

    var propertiesOfInterest = typeof (Phase).GetProperties(BindingFlags.Public | BindingFlags.Instance)
    .Where(prop => prop.Name == "StartToTerminationBars" 
        || prop.Name == "StartToTerminationPriceChange" 
        || prop.Name == "StartToTerminationGa")
    .ToList();
    //propertiesOfInterest.Dump();

    // Please Help...
    var test = propertiesOfInterest
        .SelectMany(propertyInfo => phases
            .Select(phase => phase)
            .Select(keyValuePair => new
            {
                phase.Direction,
                keyValuePair.Key,
                keyValuePair.Value
            })
            .Select(arg => new
            {
                Key = new BmkKey
                {
                    Direction,
                    DetailType = propertyInfo.Name
                },
                Value = (double)propertyInfo.GetValue(arg.Value, null)
            }))
        .GroupBy(grp => grp.Key)
        .ToDictionary(grp => grp.Key, grp => x => x.ToList());

    test.Dump();

}

预期输出:

在此处输入图像描述

标签: c#linqreflection

解决方案


解决方案

更换以下零件

//请帮忙...

具有以下内容:

    var test = propertiesOfInterest
        .SelectMany(propertyInfo => phases
            .Select(phase => new
            {
                phase.Direction,
                propertyInfo.Name,
                Value = (double)propertyInfo.GetValue(phase)
            }))
        .GroupBy(o => new BmkKey { Direction = o.Direction, DetailType = o.Name })
        .ToDictionary(grp => grp.Key, grp => grp.Select(x => x.Value));

这基本上给出了您的问题中显示的预期输出。(顺序不同;根据需要使用 OrderBy() 进行调整。)

解释

您的代码中存在一些问题,但主要问题是:

  1. 对错误对象调用GetValue ()
  2. .ToDictionary()调用 中的键选择器。

此外,以下内容没有错,但不必要:

  1. .Select ( phase => phase)调用,它什么都不做——就像一行代码指定x = x;
  2. 在分组之前构建密钥。相反,您可以在.GroupBy()操作期间简单地定义键。

推荐阅读