简体   繁体   English

c#linq查询具有其他列值的聚合数据

[英]c# linq query aggregation data with other column value

this is my environment. 这是我的环境。

I have a datatable with this kind of structure 我有一个具有这种结构的数据表

IDstring | IDstring | Attrib1 | Attrib1 | Attrib2 | Attrib2 | Attrib3 | Attrib3 | Attrib4 | Attrib4 | Value

I need to export average, max, min value group by attrib elements, and I use following code: 我需要通过attrib元素导出平均值,最大值,最小值组,并使用以下代码:

        var queryTable = from rows in dataTable.AsEnumerable()
                         group rows by new
                         {
                             Attrib1 = rows["Attrib1"],
                             Attrib2 = rows["Attrib2"],
                             Attrib3 = rows["Attrib3"],
                             Attrib4 = rows["Attrib4"]
                         } into grp
                         select new
                         {
                             Attrib1 = grp.Key.Attrib1,
                             Attrib2 = grp.Key.Attrib2,
                             Attrib3 = grp.Key.Attrib4,
                             Attrib4 = grp.Key.Attrib14,
                             Avg = grp.Average(s => Convert.ToDouble(s["Value"])),
                             Min = grp.Min(s => Convert.ToDouble(s["Value"])),
                             Max = grp.Max(s => Convert.ToDouble(s["Value"])),
                             Count = grp.Count()
                         };

If I need also to export one IDstring (not necessary all occurrence) that match max and min value, how can I do it? 如果我还需要导出一个与最大值和最小值匹配的IDstring(不必全部出现),该怎么办?

I have tried with another linq query for every max and min element of upon querytable results to the original datatable but it is too slow for the number of elements I have. 我已经尝试使用另一个linq查询来查询基于querytable结果到原始数据表的每个max和min元素,但是对于我拥有的元素数量来说太慢了。 Can you help me please? 你能帮我吗?

With the big help of coder of code, this could be the solution: First I make on order by Value and then i take first and last element. 在代码编码器的大力帮助下,这可能是解决方案:首先,我按Value定单,然后取第一个和最后一个元素。

        var queryTable = from rows in resultTable.AsEnumerable()
                         orderby rows.Field<double>("Value")
                         group rows by new
                         {
                             Attrib1 = rows["Attrib1"],
                             Attrib2 = rows["Attrib2"],
                             Attrib3 = rows["Attrib3"],
                             Attrib4 = rows["Attrib4"]
                         } into grp
                         select new
                         {
                             Attrib1 = grp.Key.Attrib1,
                             Attrib2 = grp.Key.Attrib2,
                             Attrib3 = grp.Key.Attrib3,
                             Attrib4 = grp.Key.Attrib4,
                             Avg = grp.Average(s => Convert.ToDouble(s["Value"])),
                             Min = grp.Min(s => Convert.ToDouble(s["Value"])),
                             IDStringMin = grp.First().Field<string>("IDstring"),
                             Max = grp.Max(s => Convert.ToDouble(s["Value"])),
                             IDStringMax = grp.Last().Field<string>("IDstring"),
                             Count = grp.Count()
                         };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM