使用Entity Framework 5和Repository模式以及Unit of Work过滤内部集合

我正在使用存储库和工作单元模式与entity framework5

我想通过“汽车”获得所有“代理商”,但只有那些在参数发送的列表中有id且属于参数发送状态的汽车。

例如

public IEnumerable GetList(int stateId, string idCarList) var ids = idTiposTarjetasList.Split(','); var intIds = ids.Select(int.Parse); 

然后我有

 Uow.Agencies.GetAll() 

 Uow.Agencies.GetAllIncluding(a => a.Cars) 

它检索IQueryable

无论如何,我可以在一个查询中检索包含其汽车的代理商,但只检索那些在intIds列表和stateId匹配stateId参数中包含id的代理商?

我已经看过这个Stackoverflow问题了 ,但是IQueryable的检索让我遇到了麻烦。

如果我在Uow.Agencies.GetAllIncluding(c => c.Cars)中写出这个var sortedList = from x,那么就无法完成选择(无法从查询中推断出参数

这不起作用:

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

我该怎么做? 谢谢! 吉列尔莫。

您无法获取具有部分加载集合的对象(至少不在一个语句中)。 因此,您必须创建一个包含agent对象和所选汽车的类型:

 var agencies = from agency in Uow.Agencies.GetAll() select new { Agency = agency, Cars = agency.Cars.Where(c => intIds.Contains(c.Id)) };