【问题标题】:C# LINQ : Summarise specific inner class properties from outer class collectionC# LINQ:从外部类集合中总结特定的内部类属性
【发布时间】:2019-02-28 05:34:44
【问题描述】:

借鉴Loop Through An Objects Properties In C#Using LINQ to loop through inner class properties in outer class collection

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

请在下面找到我在 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# linq reflection


【解决方案1】:

解决方案

替换后面的部分

//请帮忙...

以下内容:

    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() 操作期间简单地定义键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2015-06-09
    • 2014-08-10
    • 2011-03-10
    相关资源
    最近更新 更多