我正在尝试学习LINQ,我去了几篇有关同一问题的文章,但是无法解决这个问题。
如何在LINQ中编写以下查询?
SELECT 1
FROM A
WHERE NOT EXISTS (SELECT 1
FROM B
JOIN C ON C.id = B.c_id
JOIN D ON D.id = C.d_id
WHERE A.b_id = B.id
AND D.Active = 1
GROUP BY B.id
HAVING COUNT(D.id) >= 5)
编辑:我调查了Group by in LINQ,但不确定在分组依据并选择caluses之后如何访问B.c_id。我如何加入表C和D?
答案 0 :(得分:1)
尝试以下。我使用了类以使其更容易理解
class Program
{
static void Main(string[] args)
{
List<A> aS = new List<A>();
List<B> bS = new List<B>();
List<C> cS = new List<C>();
List<D> dS = new List<D>();
var temp = (from b in bS
join c in cS on b.id equals c.id
join d in dS on b.id equals d.id
select new { b = b, c = c, d = d })
.GroupBy(x => x.b.id)
.Where(x => x.Any(y => y.d.Active == 1) && (x.Count() >= 5))
.ToList();
var results = aS.Where(x => !temp.Any(y => y.First().b.id == x.id)).ToList();
}
}
public class A
{
public int id { get; set; }
}
public class B
{
public int id { get; set; }
}
public class C
{
public int id { get; set; }
}
public class D
{
public int id { get; set; }
public int Active { get; set; }
}