I would like to query a database table. I wonder if I can use projection by passing an expression to DbSet.
Here is my query:
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();
Here is what I want;
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();
}
And calling this method based on my query projection:
//Query GameBank database
var gameBankResult =
await _unitOfWork.GameBankRepository.GetGamesAsync(g =>
g.productCode == requestDto.productCode && g.referenceId == null, t => ...);
I just omit the select (projection) portion and implement a method for generic repository as follows:
public virtual async Task<List<TEntity>> GetGamesAsync(
Expression<Func<TEntity, bool>> where)
{
return await dbSet.Where(where).ToListAsync();
}
The I call it in my business service layer:
//Query GameBank database
var gameBankResult =
await _unitOfWork.GameBankRepository.GetGamesAsync(g =>
g.productCode == requestDto.productCode && g.referenceId == null);
And here is the solution to my problem, I used Automapper in order to make the conversion.
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);
Now all I need is to use Unity to tidy up all af there mappings around :) Hope someone help me.
Definitely I need something like below:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.