繁体   English   中英

是否可以将 select 作为表达式传递以进行投影 DbSet?

[英]Is it possible to pass select as an expression in order to make projection DbSet?

我想查询一个数据库表。 我想知道是否可以通过将表达式传递给 DbSet 来使用投影。

这是我的查询:

 var gameBankResultVM = await (context.GameBanks
                .Where(l => l.referenceId == confirm.referenceId)
                .Select(g => new GameBankConfirmResponseVM()
                {
                    referenceId = g.referenceId,
                    paymentId = null,
                    productCode = g.productCode,
                    quantity = g.quantity,
                    deliveredQuantity = g.quantity,
                    currency = g.currency,
                    version = g.version,
                    signature = g.signature,
                    ApplicationCode = g.ApplicationCode,
                    productDescription = g.productDescription,
                    unitPrice = g.unitPrice,
                    totalPrice = g.totalPrice,
                    totalPayablePrice = g.totalPrice,
                    coupons = g.coupons.Select(c => new GameCouponBankVM()
                    {
                        Pin = c.Pin,
                        Serial = c.Serial,
                        expiryDate = c.expiryDate
                    }).ToList()
                })).ToListAsync();

这是我想要的;

public virtual async Task<List<TEntity>> GetGamesProjectionAsync(
            Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, bool>> select)
        {
            return await dbSet.Where(where).Select(select).ToListAsync();
        }

并根据我的查询投影调用此方法:

//Query GameBank database
                var gameBankResult =
                    await _unitOfWork.GameBankRepository.GetGamesAsync(g =>
                        g.productCode == requestDto.productCode && g.referenceId == null, t => ...);

我只是省略了选择(投影)部分并为通用存储库实现了一个方法,如下所示:

public virtual async Task<List<TEntity>> GetGamesAsync(
        Expression<Func<TEntity, bool>> where)
    {
        return await dbSet.Where(where).ToListAsync();
    }

我在我的业务服务层调用它:

//Query GameBank database
            var gameBankResult =
                await _unitOfWork.GameBankRepository.GetGamesAsync(g =>
                    g.productCode == requestDto.productCode && g.referenceId == null);

这是我的问题的解决方案,我使用 Automapper 进行转换。

    var config = new MapperConfiguration(cfg =>
                {

                    cfg.CreateMap<GameBank, GameConfirmResponse>();
                    cfg.CreateMap<GameBankPin, Coupon>();
                });
                var iMapper = config.CreateMapper();
var gameBankConfirmResponse = iMapper.Map<IList<GameBank>, IList<GameConfirmResponse>>(gameBankConfirmResult);

现在我所需要的就是使用 Unity 来整理周围的所有映射 :) 希望有人帮助我。

当然我需要像下面这样的东西:

分组到 Repository EF C# 中的泛型方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM