使用Entity Framework 5和Repository模式和工作单元过滤内部集合 [英] Filtering inner collection with Entity Framework 5 and Repository pattern and Unit of Work
问题描述
我想把所有的代理商与其汽车,但只有那些汽车在列表中有一个id,由参数发送,属于由参数发送的状态。
例如
public IEnumerable< Agency> 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< T>
有没有办法,我可以在一个查询中检索包含其汽车的代理商,但只有那些具有包含在intIds列表中的id,stateId匹配stateId参数? / p>
我已经看到这个 Stackoverflow问题,但检索IQueryable是让我麻烦让它工作。
如果我写这个
var sortedList = from x in Uow.Agencies.GetAllIncluding(c => c.Cars)
然后select不能完成(说不可以从查询推断参数
这不起作用:
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)
选择一个gency;
if(agencies.Any())
{
return agencies;
}
我该怎么办?
谢谢! Guillermo。
您无法使用部分加载的集合获取对象(至少不在一个语句中)。因此,您必须创建一个包含代理
对象和所选车辆的类型:
I'm using the Repository and Unit Of Work Pattern with 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.
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 >
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?
I've already seen this Stackoverflow question, but the retrieval of the IQueryable is getting me troubles to get it work.
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
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:
var agencies = from agency in Uow.Agencies.GetAll()
select new { Agency = agency,
Cars = agency.Cars.Where(c => intIds.Contains(c.Id))
};
这篇关于使用Entity Framework 5和Repository模式和工作单元过滤内部集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!