簡體   English   中英

將 SQL 轉換為 LINQ JOIN/GROUP BY/COUNT/DISTINCT

[英]Convert SQL to LINQ JOIN/GROUP BY/COUNT/DISTINCT

我有一個相當困難的查詢,要從 SQL 轉換為 LINQ-to-Entities。 這是我的 SQL 代碼:

select c_id
from db.c 
inner join db.i on c_id = i_c
inner join db.l on c_id = l_c
group by c_id
having count(distinct i_attributeX) > count(distinct l_attributeY)

我似乎對 linq 中的 distinct 有問題。 有什么建議么?

干杯

這個怎么樣:

var result = db.c
    .Where(c => 
        c.i.Select(i => i.attributeX).Distinct().Count() >
        c.l.Select(l => l.attributeY).Distinct().Count()
    ) 
    .Select(c => c.id);

或者

var result = db.c
    .Where(c => 
        c.i.GroupBy(i => i.attributeX).Count() >
        c.l.GroupBy(l => l.attributeY).Count()
    ) 
    .Select(c => c.id);

暫無
暫無

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

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