.net 如何在LINQ中使用Projection将实体转换为DTO类

ldioqlga  于 2023-05-08  发布在  .NET
关注(0)|答案(3)|浏览(164)

我有Linq脚本,我想使用投影类获得数据的DTO类型。我得到了lambda expersion的例子,但在LINQ脚本上出错。
Linq脚本:

public class EziTransactionDto
{
   ... other properties

 public static Expression<Func<EziTransactionEntity, EziTransactionDto>> Projection()
    {
        return eziTransactionDto => new EziTransactionDto
        {
            EziTransactionId = eziTransactionDto.Id,
            LoginSiteID = eziTransactionDto.LoginSiteID,
            WorkCodes = eziTransactionDto.WorkCodes
        };

    }

Linq查询:

var ts = (from transaction in _eziTransactionRepository.GetAll<EziTransactionEntity>()
                  where transaction.LoginErrorCode != 0
                  select transaction
                 ).Select(EziTransactionDto.Projection);

错误:

j5fpnvbx

j5fpnvbx1#

我猜关键字Expression在这里是奇怪的。
试试这个:

// public static Expression<Func<EziTransactionEntity, EziTransactionDto>> Projection()
public static Func<EziTransactionEntity, EziTransactionDto> Projection()
{
    return eziTransactionDto => new EziTransactionDto
    {
        EziTransactionId = eziTransactionDto.Id,
        LoginSiteID = eziTransactionDto.LoginSiteID,
        WorkCodes = eziTransactionDto.WorkCodes
    };
}
hsvhsicv

hsvhsicv2#

在第一次选择之后,IQueryable已经将数据提取到本地进程,并使其成为IEnumerable。
您可以在Select语句中执行此转换:

var eziTransactionDtos = _eziTransactionRepository.EziTransactionEntities
    .Where(eziTransactionEntity => eziTransationEntity.LoginErrorCode != 0)
    .Select(eziTransactionEntity => new EziTransactionDto
    {
        EziTransactionId = eziTransactionDto.Id,
        LoginSiteID = eziTransactionDto.LoginSiteID,
        WorkCodes = eziTransactionDto.WorkCodes,
    });

但是,如果您需要在多个地方将EziTransactionEntities转换为EziTransactionDtos,那么为IQueryable<EziTransactionEntities>创建一个可重用的扩展方法是一个好主意。
如果您不熟悉扩展方法,请参阅extension methods demystified

public static IQueryable<EziTransactionDto> ToEziTransactionDto(
    this IQueryable<EziTransactionEntity> eziTransactionEntities)
{
    return eziTransactionEntities.Select(eziTransactionEntity => new EziTransactionDto
    {
        EziTransactionId = eziTransactionDto.Id,
        LoginSiteID = eziTransactionDto.LoginSiteID,
        WorkCodes = eziTransactionDto.WorkCodes,
    });

使用方法:

var eziTransactionDtos = eziTransactionRepository.EziTransactionEntities
    .Where(eziTransactionEntity => eziTransationEntity.LoginErrorCode != 0)
    .ToEziTransactionDtos();

可重复使用:

var transactionWithoutWorkCodes = eziTransactionRepository.EziTransactionEntities
    .Where(eziTransactionEntity => eziTransationEntity.WorkCode == null)
    .ToEziTransactionDtos();

易于单元测试:

List<EziTransactionEntity> testItems = ...
List<EziTransactionDto> expectedResults = ...

var testResults = testItems.AsQueryable().ToEziTransactionDtos();
Assert.AreQual(expectedResults, testResults, unorderedTransactionsComparer);

易于维护:如果添加/删除/更改此转换一个属性,则只需在一个位置执行此操作

k2arahey

k2arahey3#

寻找答案

public static Expression<Func<EziTransactionEntity, EziTransactionDto>> Projection
    {
        get
        {
            return eziTransactionDto => new EziTransactionDto
            {
                EziTransactionId = eziTransactionDto.Id,
                LoginSiteID = eziTransactionDto.LoginSiteID,
                WorkCodes = eziTransactionDto.WorkCodes
            };
        }
    }

..

var ts = (from transaction in _eziTransactionRepository.GetAll<EziTransactionEntity>()
                  select transaction
                 ).Select(EziTransactionDto.Projection).ToList();

相关问题