且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取与部分加载的集合实体框架对象

更新时间:2023-02-13 13:59:32


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


块引用>

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

  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.