2010-12-15 432 views
0

請告訴我如何將這個SQL查詢轉換成LINQ To SQL?提前致謝。SQL查詢LINQ To SQL等效

SELECT [Movies].[Name] 
FROM 
(
    SELECT [Value] 
    FROM [Index] 
    WHERE ('WORD1' = [Word] and [MatchCount] = 1) 
    OR ([Word] = 'WORD2' AND [MatchCount] = 1) 
    GROUP BY [Value] 
    HAVING COUNT([Value]) = 2 
) AS [Guids] 
LEFT OUTER JOIN [Movies] ON [Movies].[Guid] = [Guids].[Value] 

回答

1

是這樣的? (警告,以下代碼未經測試!)

var q = from i in Index 
    where i.MatchCount == 1 && 
    (i.Word == "WORD1" || i.Word == "WORD2") 
    group i by i.Value into g 
    where g.Count() == 2 
    from m in Movies.Where(x => x.Guid == g.Key).DefaultIfEmpty() 
    select m.Name; 
+0

謝謝!這正是我所期待的。 – 2010-12-16 03:19:47

1

a previous question,有人建議Linqer

我從來沒有用過它,老實說,但我已經廣泛使用LinqPad。它做相反的(以及轉換LINQ到lambda表達式),並且是我最喜歡的工具之一。

+0

謝謝您的回覆。我嘗試了Linqer,但對於給定的查詢,它生成了一個非常困難的解決方案,它比SQL查詢執行時間要長得多。 – 2010-12-15 18:49:27

+0

我會在LinqPad中驗證Linqer查詢,以查看從該LINQ語句生成的SQL是否與您上面提供的SQL查詢匹配。 – jwiscarson 2010-12-15 18:51:29