【问题标题】:Entity Framework 6 - Select Parents Where Children Equal实体框架 6 - 选择孩子平等的父母
【发布时间】:2014-05-09 18:10:52
【问题描述】:

我只需要 Parent 对象。在 SQL 中,这很简单:

select distinct * from parent 
join child on child.ParentID = Parent.ID 
where child.playssoccer = true;

在 Entity Framework 6 中,这对我来说似乎是分裂原子。

我需要新的 p => parent where parents.children.playssoccer = true 。

如何让足球父母脱离类似的 EF6 DBContext?

【问题讨论】:

  • 谢谢大家! Paul Abbott 的回答对我有用,所以我给他打勾。每个人都会投票,寿。感谢及时回复。
  • 其实你的问题不是关于 EF ,而是关于 Linq 的使用:)

标签: c# linq entity-framework-6


【解决方案1】:
from p in context.Parents
where p.Children.Any(c => c.PlaySoccer == true)
select p

这是假设您希望父母至少有一个孩子会踢足球。

【讨论】:

    【解决方案2】:

    如果你有导航属性,你可以做类似的事情

    Parents.Where(p => p.child.playsoccer)
    

    【讨论】:

      【解决方案3】:
      Parents
      .Where(p=> p.child.playsoccer)
      .GroupBy(p=> p.Parent.ID)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-28
        相关资源
        最近更新 更多