简体   繁体   English

具有组计数的 LINQ 查询

[英]LINQ query with group count

I'm trying to produce a linq sentence but I got stuck.我正在尝试生成一个 linq 语句,但我被卡住了。 The SQL I want to mimic is:我想模仿的 SQL 是:

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 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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