问题描述
class Farm {
string owner;
列表<动物>动物;
DateTime StartDate;
}
class Animal {
string Name;
DateTime DOB;
}
问题:
我想选择 我已经尝试了以下内容: Try1 : Try2 : Try3 : 阅读完毕后: 基本上,查询是选择父实体和由实体过滤的子实体,通过Relationship Fixup知道实体框架所选择的孩子与所选择的父母相关联,因此它们也被添加到父集合中。我看到这是一个奇怪的解决方案,但它的确有效。 - Andrei D。 以下作为两个单独的查询: 分解: 在执行时,查询将仅包含上述内容的数据,因此结果将包含每个 Farm ,包括过滤的动物。 Distinct 过滤副本。 I have an entity Framework model with the following: 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. Try1: Try2: Try3: After reading this: Ef-query-with-conditional-include 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. Look at the following as two separate queries: Broken out: 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. 这篇关于使用部分加载的集合获取实体框架对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
//这仍然显示每个农场的所有动物,如果有是至少一个
//动物与所需的DOB
var x = context.Farm.Where(y => y.StartDate> = myDate
&& ; y.Animal.Any(z => z.DOB> = otherDate)
).Include(Animal);
//我将Farm类子类化,因为我不能直接从Entity Framework实例化类
//,这应该是我的返回类型。
class Temp:Farm {}
var x = context.Farm.Where(y => y.StartDate> = myDate).Include(Animal)
。选择(z => new Temp(){
owner = z.owner,
animals = new TrackableCollection< Animal>(){z.animals.Where(y => y.DOB> ; = newDate).SingleOrDefault()});
//这里的一些事情:
// 1:我实例化了一个新的TrackableCollection,因为这是Animal的集合
//类型在Entity Framework中。
// 2:由于某种原因,这仍然无法工作,如果我使用这种方法,农场中的动物列表
//附带0个元素。
var x =(from farm in ctx.Farm
from animal in farm.Animal
其中animal.DOB => newDate
select new {farm,animal})AsEnumerable()选择(x => x.farm).Distinct()ToList();
//我不知道这是如何工作的,但它是...
任何人小心解释上述如何工作?
var x =(from farm in ctx.Farm
from animal in farm。 Animal
其中,animal.DOB => newDate
select new {farm,animal})AsEnumerable()选择(x => x.farm).Distinct()ToList();
//给我所有农场
从农场ctx.Farm
//给我农场一个DOB大于或等于newDate
的动物动物在farm.Animal
其中,animal.DOB => newDate
//选择两者,以便在执行期间都不会从查询中被排除
选择新的{farm,animal})
class Farm{
string owner;
List<Animal> animals;
DateTime StartDate;
}
class Animal{
string Name;
DateTime DOB;
}
Problem:
I've tried the following:
//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");
//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.
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?
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();
//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})