[英]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 來整理周圍的所有映射 :) 希望有人幫助我。
當然我需要像下面這樣的東西:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.