c# - 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();
}
预期输出:
解决方案
解决方案
更换以下零件
//请帮忙...
具有以下内容:
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() 进行调整。)
解释
您的代码中存在一些问题,但主要问题是:
- 对错误对象调用GetValue ()
- .ToDictionary()调用 中的键选择器。
此外,以下内容没有错,但不必要:
- .Select ( phase => phase)调用,它什么都不做——就像一行代码指定x = x;
- 在分组之前构建密钥。相反,您可以在.GroupBy()操作期间简单地定义键。
推荐阅读
- javascript - 对象可能是查找函数方法中的“未定义”打字稿
- python - 如何在 yaxis matplotlib 中添加文本“H_m”而不是 100%?
- flask - Flask 默认 http 标头 - 缓存等
- r - R version 4.0.3: Function For calculating Means of certain column values in R returns NaN when a row filtering argument is set to 1:278 and lower
- python - How to eliminate white space with Python string formatting (right justified)?
- java - MultipartFile 的自定义约束验证注释
- javascript - 是什么导致我部署的 Heroku 应用无法打开?
- python - 在 python 中逐步过滤/平滑信号(到左边的直线到右边没有过滤)
- ansible - Serial execution of a role in Ansible
- javascript - 如何从引导表加载未选中复选框的页面