C#LINQ:动态创建属性和值,其中包括来自分组查询的值的总和 [英] C# LINQ : Dynamically create property and values that includes sum of a value from a grouped query
问题描述
我的标题有点令人困惑,因此我将在这里进行解释.
My Title is slightly confusing so I will explain it here.
这是我的示例查询以及我希望实现的目标:
This is my sample query and what I wish to achieve:
var results = from row in toSearch.AsEnumerable()
group by row.Field<String>("Somename") into grp
select new
{
GroupMe = grp.Key,
foreach (var k in someDic.Keys){
TODOPROPERTYNAMEFROMk = grp.sum(x => grp.k);
}
};
这是我想要实现的一般想法.基于键的字典,我想从中创建属性名称,同时汇总分组查询中的数据.总和将基于键的值.
toSearch是一个数据表,字典的所有键构成该数据表的某些标头.在我看来,这似乎是不可能的,我已经搜索了一些资源,其中一个(我很抱歉失去了作者……)给了我这个解决方案:
This is the general idea of what I want to achieve. Based on a dictionary of keys, I want to create property names from it while summing up the data from the grouped query. The sum will be based off the value of the keys.
toSearch is a datatable and all of the keys of the dictionary makes up some of the headers of the datatable. This to me seems impossible and I have searched up a few sources and one of it (I lost the author sorry....) gave me this solution:
//For generating dynamic objects fr
public static object DynamicProjection(object input, IEnumerable<string> properties)
{
var type = input.GetType();
dynamic dObject = new System.Dynamic.ExpandoObject();
var dDict = dObject as IDictionary<string, object>;
foreach (var p in properties)
{
var field = type.GetField(p);
if (field != null)
dDict[p] = field.GetValue(input);
var prop = type.GetProperty(p);
if (prop != null && prop.GetIndexParameters().Length == 0)
dDict[p] = prop.GetValue(input, null);
}
return dObject;
}
我试图这样使用它
var results = from row in toSearch.AsEnumerable()
group by row.Field<String>("Somename") into grp
select grp;
var test = results.Select( x => DynamicProjection(x,someDic.Keys));
这给了我一个我无法利用或至少无法调用属性的对象.我真的很感激能帮助我实现最初的追求,并为长期的工作感到抱歉.
This gave me an object which i could not utilise or at least call properties out from. I would really appreciate any help in achieving what i was originally after and sorry for the long post.
编辑 预期的输入是一个数据表,例如:
Edit Expected input is a datatable eg:
Fee | Fi | Fo | Fum
cat | ds | 2 | 93
cow | ff | 5 | 120
cat | ds | 9 | 33
cow | jk | 1 | 133
按标题分组:费用
收到的标题:Fo,Fum
预期输出:
Group by header: Fee
Headers Received: Fo, Fum
Expected output:
cat | 11 | 126
cow | 6 | 253
优胜者:母牛,所以养牛*开个玩笑哈哈
Winner: Cow , so adopt a cow *Just joking lol
推荐答案
我对ExpandoObject
不太熟悉(我的说法可能有误),您可以使用Dictionary<columnname, sum>
.
I'm not quite familiar with ExpandoObject
(my arguments may wrong), you could work with Dictionary<columnname, sum>
.
我建议返回带有标题和各自总和的Dictionary
.
I would suggest return a Dictionary
with the headers and respective sum.
// ex -- Given following header
List<string> headers = new List<string>();
headers.Add("fo");
headers.Add("fum");
DataTable dt; // Assign correct data source.
var results =dt.AsEnumerable()
.GroupBy(g=>g.Field<string>("fee"))
.Select(x=> new
{
x.Key,
dict = headers.ToDictionary(h=>h, h=>x.Sum(s=>s.Field<int>(h)))
})
.ToList();
此查询在fee
列上分组,并计算给定标题列的总和.
This query groups on fee
column and calculates sum for a given header columns.
选中此 Demo
输出:
Key=cat, column fo , Sum 11, column fum , Sum 126
Key=cow, column fo , Sum 6, column fum , Sum 253
这篇关于C#LINQ:动态创建属性和值,其中包括来自分组查询的值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!