获取与部分加载的集合实体框架对象 [英] Fetch Entity Framework objects with partially loaded collections

查看:179
本文介绍了获取与部分加载的集合实体框架对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容的实体框架模型:





<预类=郎-CS prettyprint-覆盖> 类农场{
串业主;
名单,LT;动物>动物;
日期时间起始日期;
}

类动物{
字符串名称;
DateTime的出生日期;
}



问题:



我想随着它的动物,但也选择农场的的开始日期> = 2013年1月1日的 过滤由DOB> = 2013年6月1日



我试过以下内容:



Try1 的:



<预类=郎-CS prettyprint-覆盖> //这仍然显示各农场的所有动物,如果有是至少一种
//动物具有所需DOB

变种X = context.Farm.Where(Y => y.StartDate> =数值指明MyDate
和&放大器; y.Animal.Any(Z => z.DOB> = otherDate)
).INCLUDE(动物);

Try2 的:



<预类=郎-CS prettyprint-覆盖> //我子类农场类,因为我不能从实体框架直接实例化类
//,这应该是我的返回类型。
级温度:农场{}

变种x = context.Farm.Where(Y => y.StartDate> =数值指明MyDate).INCLUDE(动物)
。选择(Z =>新建温度(){
所有者= z.owner,
组=新TrackableCollection<动物>(){z.animals.Where(Y => y.DOB> = newDate).SingleOrDefault()});

//东西夫妇在这里:
// 1:我实例化一个新的TrackableCollection,因为多数民众赞成在收集
//类动物是实体框架内。
// 2:这出于某种原因仍然不工作,如果我用这种方法,动物的农场列表中的
//带有0的元素。

Try3 的:



看完这个: EF-查询与-条件,包括



<预类=郎-CS prettyprint-覆盖> VAR X =(从农场ctx.Farm
从farm.Animal $动物b $ b,其中animal.DOB => newDate
选择新的{农场,动物})AsEnumerable()选择(X =方式>。x.farm).Distinct()了ToList();
//我不知道这是如何工作的,但它确实...



任何人关心解释如何上述工程?



基本上查询选择通过关系修正内容父实体和所需的参数过滤子实体和实体框架知道所选的儿童与选定的父母相关联,所以它们被添加到父集合为好。我看到它那种哈克解决方案,但它的工作原理的确



- 安德烈ð


解决方案

有人在乎解释如何上述工程?




看以下为两个单独的查询:

  VAR X =(从农场ctx.Farm 
农场动物。动物
,其中animal.DOB => newDate
选择新的{农场,动物})AsEnumerable()选择(X =方式>。x.farm).Distinct()了ToList();



爆发了:

  //给我从农场在ctx.Farm 

所有农场
//给我养殖场,动物从出生日期大于或等于newDate
动物farm.Animal
,其中animal.DOB =>因此,无论是从在查询过程中执行
discluded newDate

//选择两个选择新的{农场,动物})

目前执行的角度来看,查询将只包含什么上面的数据,这样的结果将包含两个每个农用,包括过滤动物



鲜明过滤重复的。


I have an entity Framework model with the following:

class Farm{
    string owner;
    List<Animal> animals;
    DateTime StartDate;
}

class Animal{
    string Name;
    DateTime DOB;
}

Problem:

I would like to select a collection of farms whose start date is >= 2013/01/01 along with it's animals, but also filtered by DOB >= 2013/06/01.

I've tried the following:

Try1:

//This still shows all animals from each farm, if there is at least one
//animal with the required DOB

var x = context.Farm.Where(y => y.StartDate >= myDate 
                           && y.Animal.Any(z => z.DOB >= otherDate)
                          ).Include("Animal");

Try2:

//I subclassed the Farm class because i cant instantiate the class 
//from Entity Framework directly, and that should be my return type.
class Temp:Farm{}

var x = context.Farm.Where(y => y.StartDate >= myDate).Include("Animal")
        .Select(z => new Temp(){ 
                    owner = z.owner, 
                    animals = new TrackableCollection<Animal>(){ z.animals.Where(y => y.DOB >= newDate).SingleOrDefault() });

//Couple of things here:
//1: I instantiated a new TrackableCollection because thats what the collection
//type of Animal is inside Entity Framework.
//2: This still doesnt work for some reason, if i use this approach, the list 
//of animals in the farm comes with 0 elements.

Try3:

After reading this: Ef-query-with-conditional-include

var x = (from farm in ctx.Farm
        from animal in farm.Animal
        where animal.DOB => newDate
        select new{farm, animal}).AsEnumerable().Select(x=> x.farm).Distinct().ToList();
//I have no idea how this works, but it does... 

Anyone care to explain how the above works?

Basically the query is selecting the parent entity and the child entity filtered by the required parameters, and entity framework through "Relationship Fixup" knows that the selected children are associated with the selected parents, so they get added to the parent collection as well. I see it kind of a hacky solution, but it works indeed.

--Andrei D.

解决方案

Anyone care to explain how the above works?

Look at the following as two separate queries:

var x = (from farm in ctx.Farm
        from animal in farm.Animal
        where animal.DOB => newDate
        select new{farm, animal}).AsEnumerable().Select(x=> x.farm).Distinct().ToList();

Broken out:

//Give me all farms
from farm in ctx.Farm

//Give me farms with animals with a DOB greater or equal to newDate
from animal in farm.Animal
where animal.DOB => newDate

//Select both so that neither are discluded from the query during execution
select new{farm, animal})

At the point of execution, the query will only contain data from whats above, so the result will contain two of every Farm, including the filtered Animals.

The Distinct filters the duplicates.

这篇关于获取与部分加载的集合实体框架对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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