【发布时间】:2012-03-16 16:57:09
【问题描述】:
我在Entity Framework中写了如下查询,值不一样。
var query = from p in db.Parents
let children = p.Children
let grandchildren = children.SelectMany(c => c.Grandchildren)
select new
{
Count1 = children.Count(c => !c.Grandchildren.Any()),
Count2 = children.Count(c => !grandchildren.Any())
};
Count1 属性返回我所期望的,而 Count2 没有。
我正在尝试返回没有任何 Grandchild 对象的 Children 的数量。在这种情况下,Count2 似乎返回 0,但 Count1 使用以下数据集返回 1:
父母
Id
------
1
孩子
Id ParentId
--------------
1 1
2 1
孙子
Id ChildId
-------------
1 1
我有两个孩子,其中只有一个有孩子。为什么第二个查询似乎不像我认为的那样工作?
我的对象如下:
public class Parent {
public int Id { get; set; }
public virtual ICollection<Child> Children { get; set; }
}
public class Child {
public int Id { get; set; }
public int ParentId { get; set; }
public virtual ICollection<Grandchild> Grandchildren { get; set; }
}
public class Grandchild {
public int Id { get; set; }
public int ChildId { get; set; }
}
【问题讨论】:
标签: c# linq entity-framework linq-to-entities