[英]Filtering inner collection with Entity Framework 5 and Repository pattern and Unit of Work
I'm using the Repository and Unit Of Work Pattern with Entity Framework 5 我在Entity Framework 5中使用存储库和工作单元模式
I want to get all the "Agencies" with its "Cars" but only those Cars that have an id in the list sent by parameter and that belongs to the state sent by parameter. 我想获取所有带有其“汽车”的“代理商”,但仅获得那些在由参数发送的列表中具有ID且属于由参数发送的状态的汽车。
For example 例如
public IEnumerable<Agency> GetList(int stateId, string idCarList)
var ids = idTiposTarjetasList.Split(',');
var intIds = ids.Select(int.Parse);
Then I have 那我有
Uow.Agencies.GetAll()
and 和
Uow.Agencies.GetAllIncluding(a => a.Cars)
which retrieves an IQueryable< T > 检索一个IQueryable <T>
Is there anyway I can retrieve in one query the Agencies containing its Cars but only those that have an id contained in the intIds list and stateId matching stateId parameter? 无论如何,我是否可以在一个查询中检索包含其Cars的代理商,但仅检索具有intIds列表中的ID和与stateId参数匹配的stateId的代理商?
I've already seen this Stackoverflow question , but the retrieval of the IQueryable is getting me troubles to get it work. 我已经看过这个Stackoverflow问题 ,但是IQueryable的检索却使我难以工作。
If I write this var sortedList = from x in Uow.Agencies.GetAllIncluding(c => c.Cars) then the select can't be done (says arguments cannot be inferred from the query 如果我在Uow.Agencies.GetAllInclusion(c => c.Cars)中从x编写此var sortedList =,则无法完成选择(说不能从查询中推论出参数)
This doesn't work: 这不起作用:
var ids = idCars.Split(',');
var intIds = ids.Select(int.Parse);
var agencies = from agency in
Uow.Agencies.GetAllIncluding(c => c.Cars).Where(c => intIds.Contains(c.Cars.Id)).OrderBy(s => s.Id)
select agency;
if (agencies.Any())
{
return agencies;
}
How can I do it? 我该怎么做? Thanks!
谢谢! Guillermo.
吉列尔莫。
You can't fetch objects with partly loaded collections (at least, not in one statement). 您不能获取具有部分加载的集合的对象(至少不是在一条语句中)。 So you must create a type that contains the
agent
object and the selected cars separately: 因此,您必须创建一个分别包含
agent
对象和所选汽车的类型:
var agencies = from agency in Uow.Agencies.GetAll()
select new { Agency = agency,
Cars = agency.Cars.Where(c => intIds.Contains(c.Id))
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.