2017-09-27 177 views
0

我在SQL中有以下查詢。我想要從linq到sql的相同操作。如何將左連接SQL查詢轉換爲LINQ到SQL

select count(bs.sampleId),s.sampleCode from prod.Samples s 
LEFT join prod.BlockedSamples bs on bs.sampleId = s.sampleId 
group by s.sampleCode 
having count(bs.sampleId)>0 

這兩張表之間的關係是1到很多。 1個樣品可以在封閉樣品中具有多個條目。

回答

2

什麼

from s in context.Samples 
    join bs in context.BlockedSamples on s.sampleId 
    equals bs.sampleId into ps 
    from p in ps.DefaultIfEmpty() 
group p by s.sampleCode into grouped where grouped.Count(t=>t.sampleId != null)>0 
select new {key=grouped.Key,Count = 
grouped.Count(t=>t.sampleId != null)}