选择不在LINQ的“分组依据"子句中的字段 [英] Select a field that is not in Group By clause in LINQ

查看:72
本文介绍了选择不在LINQ的“分组依据"子句中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,有人可以帮我吗
如何选择不在LINQ的Group By子句中的字段"

Hi everyone, could anybody help me about
"How to select a field that is not in Group By clause in LINQ"

var ReportData = from a in context.Table1
                                  from b in context.Table2

                                  where a.personelID == b.ID
                                  && a.DateField.Value.Year == 2011
                                  group a by new { a.personelID, b.Name, b.Surname} into grouping
                                  select new
                                  {
                                       // grouping.Key.DateField,
                                      Name= grouping.Key.Name,
                                      Surname= grouping.Key.Surname,
                                      PagaBruto =  grouping.Sum(i => i.Bruto)),


                                  };


我无法选择表1中的字段"DateField",我不想在分组依据"中使用此字段,因为会得到错误的结果.
谢谢!


I can''t select the field "DateField" that is in Table1, I don''t want to have this field in Group By, because I will get a wrong result.
THANKS!

推荐答案

gazmendmehmeti-20分钟前
您可以通过以下方式进行操作:
gazmendmehmeti - 20 mins ago
You can do it in this way:
var ReportData = from a in context.Table1
                                  from b in context.Table2 
                                  where a.personelID == b.ID
                                  && a.DateField.Value.Year == 2011
                                  group a by new { a.personelID, b.Name, b.Surname} into grouping
                                  select new
                                  {
                                       // grouping.Key.DateField,
                                      DateField = grouping.First().DateField,
                                      Name= grouping.Key.Name,
                                      Surname= grouping.Key.Surname,
                                      PagaBruto =  grouping.Sum(i => i.Bruto))
                                  };


谢谢!
G.M.


THANKS!
G.M.


如果您不希望分组字段中的DateField,则意味着对于每个分组行,DateField可以有多个值.对于这些值,您必须选择要使用的汇总,总和/最小值/最大值等.
If you don''t want the DateField in the group by section, then it means that for each grouped row you can have multiple values for the DateField. For these values you must choose what aggregate to use, sum/min/max etc.


这篇关于选择不在LINQ的“分组依据"子句中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆