简体   繁体   中英

LINQ query with group count

I'm trying to produce a linq sentence but I got stuck. The SQL I want to mimic is:

select count(table1.id) as rCount1, count(table2.id) as rCount2, table.name 
from name 
inner join table1 on table1.table_id = table.id 
inner join table2 on table2.table_id = table.id 
group by table.id

I can't realy figure it out...

Try this..

from t in table
join t1 in table1
    on t1.id equals t.id
join t2 in table2
on t2.id equals t.id
group t by t.id into g
select new
{
    rCount1 = g.Count(k => k.t1.id),
    rCount2 = g.Count(k => k.t2.id),
    name = g.key.name
}

Try this way

from a in contex.name
from b in contex.table1.where(x=>x.id==a.id)
from c in contex.table2.where(x=>x.id==a.id)
group a by a.Id into g
select new{rCount1 = g.Count(x => x.t1.id), rCount2 = g.Count(x => x.t2.id),
name = g.key.name }

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