-1

I am bit stuck with the converting a SQL query into LINQ. Can any body please help me out. Here is my query

SELECT x.*
  FROM FilterType x
  JOIN (SELECT t.FilterType
          FROM FilterType t
          where FilterId in (7,15)
      GROUP BY t.FilterType
        HAVING COUNT(t.FilterType) > 1) y ON y.FilterType = x.FilterType

Thanks in advance.

nawfal
  • 70,104
  • 56
  • 326
  • 368
gofor.net
  • 4,218
  • 10
  • 43
  • 65

2 Answers2

1

Suppose you have int[] ids = { 7, 15 }. Then query will look like:

from t in FilterType.Where(x => ids.Contains(x.FilterId))
group t by t.FilterType into g
where g.Count() > 1
from f in g
select f

Or with method syntax:

FilterType.Where(x => ids.Contains(x.FilterId))
          .GroupBy(t => t.FilterType)
          .Where(g => g.Count() > 1)
          .SelectMany(g => g);

Generated SQL will not be exactly as yours, but result should be same.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0
from a in FilterType
join b in
    (
        from x in FilterType
        where (new int[]{7, 15}).Contains(x.FilterID)
        group x by new {x.FilterType} into g
        where g.Count() > 1
        select new {FilterType = g.Key.FilterType}
    ) on a.FilterType equals b.FilterType
select a;
John Woo
  • 258,903
  • 69
  • 498
  • 492