【问题标题】:how to use group by in linq C# and fetch the list of records [duplicate]如何在linq C#中使用group by并获取记录列表[重复]
【发布时间】:2015-03-14 21:20:54
【问题描述】:

我有 2 个表,我根据某些要求进行了一些连接以返回记录列表。 我可以通过查询为我的 requiremnet 编写一个组并检查记录,但我在 Linq 查询中需要相同的内容。 我的 sql 查询是:

select 
   MiscServiceDesc, 
   sum(PaymentAmount),
   count(PaymentAmount) 
from 
   MiscTransaction MT 
join MiscService MS 
   on MT.MiscServiceID = MS.MiscServiceID 
group by 
  MiscServiceDesc

其中 MiscTransaction 和 MiscService 是我的两个表。

有什么帮助吗? 提前致谢

【问题讨论】:

标签: linq sql-to-linq-conversion


【解决方案1】:

试试这个。我不知道 MiscServiceDesc、PaymentAmount、PaymentAmount 哪些列在 MiscTransaction 表或 MiscService 表中,但您可以根据需要适当更改代码。

var result = (from mt in MiscTransaction 
               join ms in MiscService on mt.MiscServiceID equals ms.MiscServiceID
               select new {ms.MiscServiceDesc, mt.PaymentAmount} into lst
               group lst by lst.MiscServiceDesc into gr
               select new {MiscServiceDesc  = gr.Key, Summ = gr.Sum(c=>c.PaymentAmount), Count = gr.Count()}
       ).ToList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-20
    • 2011-07-11
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 2020-02-02
    相关资源
    最近更新 更多