LinqToSql过滤器EntitySet [英] LinqToSql Filter EntitySet

查看:43
本文介绍了LinqToSql过滤器EntitySet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Linq To Sql开发WP7应用程序.我使用过Linq,但这是我第一次使用Linq到Sql.我在EntitySet中过滤数据时遇到问题.我可能做错了我不知道.我现在拥有的东西可以使用,但是我需要过滤其中一个EntitySet.

I am working on a WP7 application using Linq To Sql. I have used Linq, but this is the first I have used Linq to Sql. I am having an issue with filtering data in an EntitySet. I maybe doing it wrong I have no clue. What I have right now works, but I need to get one of the EntitySets filtered.

我有4张桌子.父,子,孙和ParentChild链接表.当我查询ParentChild时,我会返回ParentChild实体,并且可以很好地遍历Parent,Child和孙子实体.我想做的是过滤孙子实体.

I have 4 tables. The Parent, Child, Grandchild, and a ParentChild linking table. When I query ParentChild, I get back the ParentChild entity and I can iterate through the Parent, Child and Grandchild entities just fine. What I want to be able to do is filter the Grandchild entity.

让我们说父母"表中有一个父母.然后我在儿童桌子上有一个儿子和女儿.然后是孙子孙女和孙女.当然有正常的关联等等.

Lets say I have a father and mother in the Parent table. Then I have a son and daughter in the Child table. Then a grandson and granddaughter in the Grandchild table. Of course there are normal associations, etc.

我想归还父亲,这也使我所有关联的表都很好.我遇到的问题是对孙子项进行过滤.假设我只想要孙子并且有一个做爱的领域.我怎样才能做到这一点?我似乎无法弄清楚.

I want to return the father, which also gets me all the associated tables just fine. The problem that I have is with filtering on the Grandchild. Let's say I want just the grandson and have a field for sex. How can I do this? I just can't seem to figure it out.

这是我正在使用的代码,可以正常工作,但是可以拉走所有的孙子.

Here is the code I am using which works fine, but it pulls all the grandchildren.

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);
}

我得到了父母,但我只得到了有孙子孙女的孩子.我想要父母,所有孩子(即使他们没有女孙子或没有任何孙子),也只有女孙子.

I get the parent, but I only get the children that have female grandchildren. I want the parent, all the children (even if they don't have female grandchildren or don't have any grandchildren) and only the female grandchildren.

推荐答案

经过反复尝试和搜索,我找到了答案.我必须使用AssociateWith选项.

After much trial and error and searching, I found the answer. I have to use the AssociateWith option.

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

this.DataContext.LoadOptions = dataLoadOptions;

这篇关于LinqToSql过滤器EntitySet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆