【问题标题】:LINQ query with group count具有组计数的 LINQ 查询
【发布时间】:2015-09-16 02:40:58
【问题描述】:

我正在尝试生成一个 linq 句子,但我卡住了。 我想模仿的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

我真的想不通...

【问题讨论】:

  • 所以展示你的努力!请记住,在大多数情况下,重要的是要知道它与哪种 LINQ 相关并且导航属性几乎总是比 LINQ 连接更好。

标签: sql .net linq


【解决方案1】:

试试这个方法

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 }

【讨论】:

    【解决方案2】:

    试试这个..

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多