我正在使用Linq To Sql的WP7應用程序。我已經使用Linq,但這是我第一次使用Linq to Sql。我在過濾EntitySet中的數據時遇到問題。我可能做錯了我不知道。我現在有的工作,但我需要得到一個過濾的EntitySets。LinqToSql篩選器實體集
我有4張桌子。 Parent,Child,Grandchild和ParentChild鏈接表。當我查詢ParentChild時,我找回了ParentChild實體,並且可以遍歷Parent,Child和Grandchild實體。我想要做的就是過濾孫子實體。
可以說我在父表中有一個父親和母親。然後我在兒童桌上有一個兒子和女兒。然後是孫子孫女中的孫子和孫女。當然,有正常的協會等等。
我想要返回父親,這也讓我所有關聯的表很好。我有的問題是過濾孫子。假設我只想要孫子,並且有一個性愛領域。我怎樣才能做到這一點?我似乎無法弄清楚。
這裏是我使用的代碼工作正常,但它拉動所有的孫子。
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> 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);
}
我得到了家長,但我只得到有女孫輩孩子。我希望父母,所有的孩子(即使他們沒有女性孫輩或沒有任何孫輩),也只有女性孫子女。
這正是我所追求的,需要更多的讚揚! – Ian 2016-01-07 02:06:07