[英]How do I combine objects in a generic list into generic class?
So I have a generic list of objects, and I need to combine their properties to get list of sums.所以我有一个通用的对象列表,我需要组合它们的属性来获取总和列表。
public class PickupReport
{
public int Units { get; set; }
public int Revenue { get; set; }
public int Accs { get; set; }
}
public class ReportBase<TReport>
{
public List<TReport> Reports { get; set; }
public List<int> Sum
{
get
{
return new List<int>{1,2,3}
}
}
}
So, if I have所以,如果我有
var reportBase = new ReportBase<PickupReport>();
reportBase.Reports = new List<PickupReport>{
new PickupReport{Units = 1, Revenue = 2, Accs = 3},
new PickupReport{Units = 4, Revenue = 5, Accs = 6},
new PickupReport{Units = 2, Revenue = 3, Accs = 4}
}
I need to get and calculate reportBase.Sum
and it should be List<int>
with {7, 10, 13}
我需要获取并计算
reportBase.Sum
,它应该是List<int>
和{7, 10, 13}
Like {the sum of property #1 for all objects, the sum of property #2 for all objects, and so on.. }
像
{the sum of property #1 for all objects, the sum of property #2 for all objects, and so on.. }
Is it possible to do this somehow, given that the number of objects in the list is dynamic?鉴于列表中的对象数量是动态的,是否可以以某种方式执行此操作? Well, the number of object properties, too, but within the class.
好吧,对象属性的数量也是如此,但在类内。
@MongZhu only int
@MongZhu 只有 int
Then I would suggest to use reflection for this approach.然后我建议对这种方法使用反射。 Make it a method rather then a property:
使它成为一个方法而不是一个属性:
public class ReportBase<TReport>
{
public List<TReport> Reports { get; set; }
public IEnumerable<int> CalculateSum()
{
foreach (var element in typeof(TReport).GetProperties())
{
if (element.PropertyType == typeof(int))
{
yield return Reports.Sum(x => (int)element.GetValue(x));
}
}
}
}
Explanation:解释:
This method iterates over all properties that are in the type and if it finds a property of type int
it will calculate the sum of it from the entire list.此方法遍历类型中的所有属性,如果找到
int
类型的属性,它将从整个列表中计算它的总和。
Here is a testprogramm and the result dump from LINQPad:这是一个测试程序和来自 LINQPad 的结果转储:
void Main()
{
var reportBase = new ReportBase<PickupReport>();
reportBase.Reports = new List<PickupReport>{
new PickupReport(1,2,3),
new PickupReport(4,5,6),
new PickupReport(2,3,4)
};
reportBase.CalculateSum().Dump();
}
EDIT:编辑:
just noticed, that objects in my project can have two types of properties, not one
刚刚注意到,我项目中的对象可以有两种类型的属性,而不是一种
Here is a version where you can add types to a list of allowed sum up types:这是一个版本,您可以在其中将类型添加到允许的汇总类型列表中:
public class ReportBase<TReport>
{
public List<TReport> Reports { get; set; }
List<Type> possibleTypes = new List<Type> {typeof(int), typeof(double)};
public IEnumerable<double> CalculateSum()
{
foreach (PropertyInfo element in typeof(TReport).GetProperties())
{
if (possibleTypes.Contains(element.PropertyType))
{
yield return Reports.Sum(x => Convert.ToDouble(element.GetValue(x)));
}
}
}
}
Now you can also handle double
现在你也可以处理
double
This should get you a List<int>
with the sums of all int
properties of TReport
:这应该为您提供一个
List<int>
,其中包含TReport
的所有int
属性的TReport
:
var properties = typeof(PickupReport).GetProperties();
List<int> sum = new List<int>();
for (int i = 0; i<properties.Length; ++i)
{
sum.Add(0);
if (properties[i].PropertyType == typeof(int))
{
foreach (var report in reportBase.Reports)
{
sum[i] += (int) properties[i].GetValue(report);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.