Boris Mitchenko
Boris Mitchenko

Reputation: 880

SQL query LINQ To SQL equivalent

Please, tell me how to convert this SQL query in LINQ To SQL? Thanks in advance.

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]

Upvotes: 0

Views: 224

Answers (2)

diceguyd30
diceguyd30

Reputation: 2752

Something like this? (Warning, following code is untested!)

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;

Upvotes: 1

jwheron
jwheron

Reputation: 2562

In a previous question, someone suggested Linqer.

I've never used it myself, honestly, but I have used LinqPad extensively. It does the opposite (as well as converting LINQ to lambda expressions), and is one of my favorite tools.

Upvotes: 1

Related Questions