简体   繁体   中英

Query with join, group by and count to linq

im doing this query on sql and it works, but how do i make it to 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

this is what it should bring

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

A straight forward translation of the SQL into LINQ yields:

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()
          };

Try:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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