【问题标题】:Map IQueryable<Entity> to IQueryable<DTO> in Linq with nested objects使用嵌套对象在 Linq 中将 IQueryable<Entity> 映射到 IQueryable<DTO>
【发布时间】:2012-08-06 19:57:34
【问题描述】:

我正在尝试使用 Automapper 将 IQueryable 映射到 IQueryable 以生成 Linq。我正在开发一个带有实体框架和 oracle 11g 的 Web API 项目。

public virtual IQueryable<TDto> Get() 
{
IQueryable<TEntity> EntObjs;
EntObjs = GenericService.Get();     
var Dtos = EntObjs.Project().To<TDto>();
return Dtos;
}

只要 Tentity 类型中没有任何集合,它就可以正常工作。我在http://www.devtrends.co.uk/blog/stop-using-automapper-in-your-data-access-code 找到了解决问题的方法。我知道我可以使用以下函数使用 Automapper 映射集合,但我需要它在 linq 中,这样我就不会破坏 Iquerable 链。

Mapper.Map<TSource, TDestination>(Source,Destincation);

【问题讨论】:

    标签: asp.net entity-framework automapper asp.net-web-api dto


    【解决方案1】:

    您可以在查询链中使用它。

    例如:

    EntObjs.Project(). Select(x=> Mapper.Map(x))...
    

    【讨论】:

    • Mapper.Map 是否破坏了 IQueryable 链?由于使用了 Odata 过滤器,我无法打破这条链。
    • 很抱歉。试试看,让我知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    相关资源
    最近更新 更多