2012-08-08 66 views
2

Linq中適用於此SQL查詢的正確語法讓我無法理解。我會很感激這一切。將SQL轉換爲Linq - Where,Groupby和Having

SQL:

SELECT TOP 1 posdate 
FROM dailypos 
GROUP BY posdate 
HAVING Count(DISTINCT customernumber) = 3 
ORDER BY posdate DESC) 

從本質上講,我從3個不同的客戶文件,我需要一個快速的方法來確定,我有所有3.數據的最近日期我開出了不同的的方法,但這個SQL適用於我所需要的。

我能做的組,但我不知道如何處理4號線(具有計數(DISTINCT ...))

在此先感謝。

回答

2

嘗試這樣:

var result = context.dailypos 
    .GroupBy(x => x.posdate) 
    .Where(g => g.Select(x => x.customernumber).Distinct().Count() == 3) 
    .Select(g => g.Key) 
    .OrderByDescending(x => x) 
    .Take(1); 
+0

絕對完美。 .where()。distinct()。Count()的序列是我掙扎的地方。現在我看到它已經佈置完成,這很有意義。 – nycdan 2012-08-09 14:14:31