C# LinqToSql过滤器实体集

C# LinqToSql过滤器实体集,c#,windows-phone-7,linq-to-sql,C#,Windows Phone 7,Linq To Sql,我正在使用LINQtoSQL开发一个WP7应用程序。我已经使用了Linq,但这是我第一次使用Linq到Sql。我在过滤EntitySet中的数据时遇到问题。我可能做错了我不知道。我现在所做的工作正常,但我需要过滤其中一个EntitySet 我有四张桌子。父、子、孙和父子链接表。当我查询ParentChild时,我返回ParentChild实体,我可以很好地遍历父、子和孙实体。我希望能够过滤孙子实体 假设我有一个父亲和母亲在父母表中。然后我有一个儿子和女儿在儿童桌上。然后是孙子和孙女在孙子桌上。当

我正在使用LINQtoSQL开发一个WP7应用程序。我已经使用了Linq,但这是我第一次使用Linq到Sql。我在过滤EntitySet中的数据时遇到问题。我可能做错了我不知道。我现在所做的工作正常,但我需要过滤其中一个EntitySet

我有四张桌子。父、子、孙和父子链接表。当我查询ParentChild时,我返回ParentChild实体,我可以很好地遍历父、子和孙实体。我希望能够过滤孙子实体

假设我有一个父亲和母亲在父母表中。然后我有一个儿子和女儿在儿童桌上。然后是孙子和孙女在孙子桌上。当然有正常的联系等等

我想返回父亲,这也让我所有相关的表都很好。我的问题是过滤孙子。比方说,我只想要孙子,有一个做爱的场地。我该怎么做?我只是想不通

下面是我正在使用的代码,它工作得很好,但是它吸引了所有的孙子

IQueryable<ParentChild> parentChild = from ParentChild c in DataContext.ParentChild
                                              where c.ParentId == this.parentId
                                              select c;

foreach (Grandchild grandchild in parentChild.SelectMany(parent => parent.Child.Grandchild))
{
     Console.WriteLine(grandchild.Name);
}
IQueryable parentChild=来自DataContext.parentChild中的parentChild c
其中c.ParentId==this.ParentId
选择c;
foreach(parentChild.SelectMany中的孙子孙女(parent=>parent.Child.grander))
{
Console.WriteLine(孙子姓名);
}
因此,如果我这样做:

IQueryable<ParentChild> parentChild = from ParentChild c in DataContext.ParentChild
                                      where c.ParentId == this.parentId && c.Child.Grandchild.Any(a => a.Sex == "F")
                                      select c;

foreach (Grandchild grandchild in parentChild.SelectMany(parent => parent.Child.Grandchild))
{
     Console.WriteLine(grandchild.Name);
}
IQueryable parentChild=来自DataContext.parentChild中的parentChild c
其中c.ParentId==this.ParentId&&c.Child.grander.Any(a=>a.Sex==“F”)
选择c;
foreach(parentChild.SelectMany中的孙子孙女(parent=>parent.Child.grander))
{
Console.WriteLine(孙子姓名);
}

我有父母,但我只有有女孙的孩子。我想要的是父母,所有的孩子(即使他们没有女孙或没有孙子),只有女孙。

只要你在SQL中正确设置了外键;LINQtoSQL将能够为您提供与外键关系匹配的关联属性

IQueryable<ParentChild> parentChild = from ParentChild c in DataContext.ParentChild
                                              where c.ParentId == this.parentId
                                              select c;

foreach (Grandchild grandchild in parentChild.SelectMany(parent => parent.Child.Grandchild))
{
     Console.WriteLine(grandchild.Name);
}
如果设置了外键,您将能够执行以下操作

var query = from p in DataContext.Parent            
            //make sure they have at least 1 female grandchild
            where p.GrandChilds.Any(gc => gc.IsFemale)
            select p;

我对您的数据模型中的名称做了一些假设,但您明白了。:-)

经过反复试验和寻找,我找到了答案。我必须使用AssociateWith选项

DataLoadOptions dataLoadOptions = new DataLoadOptions();
dataLoadOptions.AssociateWith<Child>(c => c.Grandchild.Where(p => p.Sex == "F"));

this.DataContext.LoadOptions = dataLoadOptions;
DataLoadOptions DataLoadOptions=newdataloadoptions();
dataLoadOptions.AssociateWith(c=>c.grander.Where(p=>p.Sex==“F”);
this.DataContext.LoadOptions=dataLoadOptions;

我现在唯一的问题是它很好地拉住了孙子,但它只拉住了与孙子相关的一个孩子。我希望能够为所有的孩子拉父亲、所有的孩子和所有的女孙。这正是我所追求的,需要更多的投票!