簡體   English   中英

通過連接、分組和計數查詢到 linq

[英]Query with join, group by and count to linq

我在 sql 上做這個查詢,它可以工作,但我如何讓它到 Linq?

select b.Name,a.idempresa,count(1) as cuenta from Descarga_XML_recepcion_V2 as a inner join EnterpriseSGE as b on a.idempresa = b.EnterpriseSGEId group by idempresa,b.name

這是它應該帶來的

name1 | 5041 |  583
name2 | 4031 |  1730
name3 | 5042 |  640
name4 | 4034 |  300
name5 | 6047 |  861
name6 | 5043 |  187
name7 | 4033 |  318

將 SQL 直接翻譯成 LINQ 得到:

var ans = from a in Descarga_XML_recepcion_V2
          join b in EnterpriseSGE on a.idempresa equals b.EnterpriseSGEId
          group 1 by new { a.idempresa, b.name } into ingroup
          select new {
            ingroup.Key.idempresa,
            ingroup.Key.name,
            cuenta = ingroup.Count()
          };

嘗試:

var results = (from a in Descarga_XML_recepcion_V2
   join b in EnterpriseSGE on a.idempresa equal b.EnterpriseSGEId
   select new { a = a, b = b})
   .GroupBy(x => new { idempresa = x.a.idempresa, name = x.b.name})
   .Select(x => new {name = x.Key.name, idempresa = x.Key.idempressa, count = x.Count()})
   .ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM