AutoMapper 是否支持 Linq? [英] Does AutoMapper support Linq?

查看:25
本文介绍了AutoMapper 是否支持 Linq?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对具有延迟加载功能的 Linq to SQL 非常感兴趣.在我的项目中,我使用 AutoMapper 将 DB 模型映射到域模型(从 DB_RoleInfoDO_RoleInfo).在我的存储库代码如下:

I am very interested in Linq to SQL with Lazy load feature. And in my project I used AutoMapper to map DB Model to Domain Model (from DB_RoleInfo to DO_RoleInfo). In my repository code as below:

    public DO_RoleInfo SelectByKey(Guid Key)
    {
        return SelectAll().Where(x => x.Id == Key).SingleOrDefault();
    }

    public IQueryable<DO_RoleInfo> SelectAll()
    {
        Mapper.CreateMap<DB_RoleInfo, DO_RoleInfo>();
        return from role in _ctx.DB_RoleInfo
               select Mapper.Map<DB_RoleInfo, DO_RoleInfo>(role);
    }

SelectAll 方法运行良好,但是当我调用 SelectByKey 时,出现错误:

SelectAll method is run well, but when I call SelectByKey, I get the error:

方法RealMVC.Data.DO_RoleInfo MapDB_RoleInfo,DO_RoleInfo"无法转换为 SQL.

Method "RealMVC.Data.DO_RoleInfo MapDB_RoleInfo,DO_RoleInfo" could not translate to SQL.

是不是 Automapper 不完全支持 Linq?

Is it that Automapper doesn't support Linq completely?

我尝试了下面的手动映射代码,而不是 Automapper:

Instead of Automapper, I tried the manual mapping code below:

public IQueryable<DO_RoleInfo> SelectAll()
{
    return from role in _ctx.DB_RoleInfo 
    select new DO_RoleInfo 
    {
        Id = role.id,
        name = role.name,
        code = role.code
    };
}

这个方法按照我想要的方式工作.

This method works the way I want it to.

推荐答案

虽然@Aaronaught 的回答在撰写本文时是正确的,但由于世界经常发生变化,AutoMapper 也随之改变.同时,QueryableExtensions 被添加到代码库中,增加了支持用于转换为表达式,最后转换为 SQL 的投影.

While @Aaronaught's answer was correct at the time of writing, as often the world has changed and AutoMapper with it. In the mean time, QueryableExtensions were added to the code base which added support for projections that get translated into expressions and, finally, SQL.

核心扩展方法是ProjectTo1.您的代码可能如下所示:

The core extension method is ProjectTo1. This is what your code could look like:

using AutoMapper.QueryableExtensions;

public IQueryable<DO_RoleInfo> SelectAll()
{
    Mapper.CreateMap<DB_RoleInfo, DO_RoleInfo>();
    return _ctx.DB_RoleInfo.ProjectTo<DO_RoleInfo>();
}

它的行为就像手动映射.(CreateMap 语句用于演示目的.通常,您会在应用程序启动时定义一次映射).

and it would behave like the manual mapping. (The CreateMap statement is here for demonstration purposes. Normally, you'd define mappings once at application startup).

因此,只查询映射所需的列,结果是一个 IQueryable,它仍然具有原始查询提供程序(linq-to-sql、linq-to-entities,无论如何).所以它仍然是可组合的,这将转化为 SQL 中的 WHERE 子句:

Thus, only the columns that are required for the mapping are queried and the result is an IQueryable that still has the original query provider (linq-to-sql, linq-to-entities, whatever). So it is still composable and this will translate into a WHERE clause in SQL:

SelectAll().Where(x => x.Id == Key).SingleOrDefault();

<小时>

1 Project().To() v. 4.1.0 之前


1 Project().To<T>() prior to v. 4.1.0

这篇关于AutoMapper 是否支持 Linq?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆