简体   繁体   English

使用Entity Framework 5和存储库模式和工作单元过滤内部集合

[英]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.

相关问题 实体框架,存储库模式,工作单元和测试 - Entity Framework, Repository Pattern, Unit of Work and Testing 使用Unity for Work of Unit / Repository模式创建Entity Framework对象 - Creating Entity Framework objects with Unity for Unit of Work/Repository pattern 实体框架+存储库+工作单元 - Entity Framework + Repository + Unit of Work 工作单元模式和更新实体集合属性 - Unit of Work Pattern And Updating Entity Collection Properties 使用Entity Framework创建简单的工作单元,无需存储库 - Creating a simple unit of work with Entity Framework and no repository 实体框架与Codefirst,通用存储库,工作单元模式保存多对多关系 - Entity Framework Saving Many-to-Many Relationship with Codefirst, Generic Repository, Unit of Work Pattern 工作单元+存储库模式+实体框架4:依赖记录(尚未在单笔交易中保存在数据库中) - Unit of work + repository pattern + entity framework 4 : Dependancy of records which is not yet saved in database in sigle transaction 如何使用组合主键在存储库模式工作实体框架单元中插入/删除记录 - How to insert / Delete record in repository pattern unit of work entity framework with combined primary key 使用存储库和工作单元模式进行多个DB事务的C#实体框架 - C# Entity Framework Using the Repository and Unit of Work Pattern for Multiple DB Transactions 使用Moq进行单元测试的工作单元和通用存储库模式框架 - Unit Testing Unit of Work and Generic Repository Pattern framework using Moq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM