带总和的EF Core 5 [英] EF Core 5 with Sum

查看:67
本文介绍了带总和的EF Core 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重新发布模型类:

 公共类TransactionDayReport{public Decimal TotalAmount {get;放;}公共ICollection< TransactionResponse>TransactionResponses {get;放;} = null !;} 

这是我的存储库方法

 公共异步任务< IEnumerable< Transaction>>SearchTransactionAsync(SearchTransaction搜索){var query = _context.Transactions.include(t => t.Branch).include(t => t.Customer).AsQueryable();如果(search.CIN!= null)query = query.Where(t => t.Customer.CIN.Contains(search.CIN));如果(search.ValueDate!= null)query = query.Where(t => t.ValueDate.Date == search.ValueDate.Date);返回等待查询.ToListAsync();} 

服务方法:

 公共异步任务< TransactionResponse>SearchTransactions(SearchTransaction搜索){var transactionList =等待_unitOfWork.Transactions.SearchTransactionAsync(search);var mapping = ObjectMapper.Mapper.Map< TransactionDayReport>(transactionList);返回映射} 

我需要在服务类别中发送总计金额.

看起来与此类似

但是我很困惑如何将加总和添加到我的dto中.请谁能提出解决方案?谢谢

解决方案

也在数据库上计算总和的唯一方法是在数据库上运行另一个查询,除非数据库查询可以重新编写以返回总和和数据.

如果您可以在客户端上计算总和,那就更简单了.这是我想到的一些选择:

  1. 您可以配置映射器以设置总和.
  2. 您可以在 SearchTransactions()中执行映射后设置总和.
  3. 您可以更改DTO来计算总和(在这种情况下,它不再是POCO,但是我认为这也是一种合理的方法):

     公共类TransactionDayReport{私人小数?_totalAmount = null;公开的十进制TotalAmount {get {if(_totalAmount不为null)返回_totalAmount;//新的C#9功能:)_totalAmount =//在此计算总金额返回_totalAmount;}公共ICollection< TransactionResponse>TransactionResponses {get;放;} = null !;} 

Repost model class:

public class TransactionDayReport
{
    public Decimal TotalAmount { get; set; }
    public ICollection<TransactionResponse> TransactionResponses { get; set; } = null!;
}

This is my repository method

public async Task<IEnumerable<Transaction>> SearchTransactionAsync(SearchTransaction search)
{
    var query = _context.Transactions
                        .Include(t => t.Branch)
                        .Include(t => t.Customer)
                        .AsQueryable();    

    if (search.CIN != null)               
        query = query.Where(t => t.Customer.CIN.Contains(search.CIN));

    if (search.ValueDate != null)
        query = query.Where(t => t.ValueDate.Date == search.ValueDate.Date);

    return await query.ToListAsync();
}

Service method:

public async Task<TransactionResponse> SearchTransactions(SearchTransaction search)
{
    var transactionList = await _unitOfWork.Transactions.SearchTransactionAsync(search);
    var mapped = ObjectMapper.Mapper.Map<TransactionDayReport>(transactionList);
    return mapped;
}

I need to send total amount in service class..

am looking similar to this

But I am confused how to get addition sum added to my dto. Please can anyone suggest a solution? Thanks

解决方案

The only way to also calculate the sum on the DB is to run another query on the DB, unless the DB query can be re-written to return both the sum and the data.

If you're ok with calculating the Sum on the client, it's more straightforward. Here are some options I could think of:

  1. You could configure the mapper to set the sum.
  2. You could set the sum after performing the mapping in SearchTransactions().
  3. You could change the DTO to calculate the Sum (in which case it wouldn't be a POCO anymore, but I think this is a reasonable approach too):

    public class TransactionDayReport
    {
        private decimal? _totalAmount = null;
        public decimal TotalAmount { get {
            if(_totalAmount is not null) return _totalAmount; // new C# 9 feature :)
            _totalAmount = // calculate total amount here
            return _totalAmount;
        }
        public ICollection<TransactionResponse> TransactionResponses { get; set; } = null!;
    }
    

这篇关于带总和的EF Core 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆