[英]Is it possible to pass select as an expression in order to make projection DbSet?
I would like to query a database table.我想查询一个数据库表。 I wonder if I can use projection by passing an expression to DbSet.
我想知道是否可以通过将表达式传递给 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.这是我的问题的解决方案,我使用 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);
Now all I need is to use Unity to tidy up all af there mappings around :) Hope someone help me.现在我所需要的就是使用 Unity 来整理周围的所有映射 :) 希望有人帮助我。
Definitely I need something like below:当然我需要像下面这样的东西:
grouping to generic method in Repository EF C# 分组到 Repository EF C# 中的泛型方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.