代码之家  ›  专栏  ›  技术社区  ›  jdelator

基于子属性linq to sql查找父级

  •  1
  • jdelator  · 技术社区  · 16 年前

    假设我们有一个SQL关系,可以使用C类像这样建模。

    public class Parent 
    { 
      public int ID { get; set; } 
      public List<Child> Children { get; set; } 
    } 
    
    public class Child 
    { 
      public int ID { get; set; } 
      public Parent Parent { get; set; } 
      public int Number { get; set; } 
    } 
    

    我也知道父母只有两个孩子。我会发现所有的父母都满足不同的标准吗?假设一个孩子的数字为0,另一个孩子的数字为1。

    3 回复  |  直到 16 年前
        1
  •  1
  •   James Manning    16 年前
    from p in context.Parents
    where p.Children.Count == 2 // sounds like you can skip this one
    where p.Children.Any(c => c.Number == 0)
    where p.Children.Any(c => c.Number == 1)
    select p;
    
        2
  •  2
  •   wcm    16 年前

    在这里冒险…

    (from p in context.Parents 
     where p.Children.Count == 2 
     where p.Children.Any(c => c.Number == 0) 
     select p).Where(p => p.Children.Any(c => c.Number == 1));
    
        3
  •  0
  •   Jcl    16 年前
    from o in context.Parents where o.Children.Count == 2 && o.Children.Any(x => x.Number == 0) && x.Children.Any(x => x.Number == 1) select o;