复杂高效的LINQ查询

时间:2019-03-09 13:26:54

标签: stored-procedures linq-to-sql sql-view

我写了linq query来生成不同销售点的现金簿,并按sort排列datequery需要很长时间才能完成。

步骤1:首先,我在sql server中编写了一个视图,该视图将费用和付款这两种交易类型结合在一起。

select *, 'expense' as TransactionType from  tbl_Expenses
Union
select *, 'payment' as TransactionType from tbl_Payments

第2步:我现在编写了一个linq来查询视图并生成所需的结果。

    public IList<dto_CashBookList> CashBookReport(DateTime? fromDate, DateTime? toDate, int? locationId)
    {
        try
        {
            List<dto_CashBookList> lstCashBookList = new List<dto_CashBookList>();

            var locations = locationId == 0 ? _context.vw_Cashbook.Where(t => t.TransactionDate >= fromDate && t.TransactionDate <= toDate).DistinctBy(d => d.LocationId).ToList() : _context.vw_Cashbook.Where(t => t.TransactionDate >= fromDate && t.TransactionDate <= toDate && t.LocationId == locationId).DistinctBy(d => d.LocationId).ToList();


            foreach(var location in locations)
            {

                List<dto_CashBook> lstCashBook = new List<dto_CashBook>();

                var transactions = _context.vw_Cashbook.Where(t => t.TransactionDate >= fromDate && t.TransactionDate <= toDate && t.LocationId == location.LocationId).DistinctBy(d => d.TransactionDate.Date).OrderBy(k=>k.TransactionDate).ToList();

                foreach (var transaction in transactions)
                {
                    try
                    {

                        var expense = (from x in _context.vw_Cashbook
                                       where x.TransactionType == "Expense" &&  x.TransactionDate.Year == transaction.TransactionDate.Year && x.TransactionDate.Day == transaction.TransactionDate.Day && x.TransactionDate.Month == transaction.TransactionDate.Month && x.LocationId ==transaction.LocationId
                                       select new dto_Expense()
                                       {
                                           RecieverName = x.Name,
                                           ReferenceNumber = x.ReferenceNumber,
                                           Amount = x.Amount,
                                           TransactionId = x.TransactionId,
                                           PaymentModeId = x.PaymentModeId,
                                           TransactionDate = x.TransactionDate
                                       });


                        var payment = (from x in _context.vw_Cashbook
                                       where x.TransactionType == "Payment" &&  x.TransactionDate.Year == transaction.TransactionDate.Year && x.TransactionDate.Day == transaction.TransactionDate.Day && x.TransactionDate.Month == transaction.TransactionDate.Month && x.LocationId == transaction.LocationId
                                       select new dto_Payment()
                                       {
                                           Amount = x.Amount,
                                           TransactionId = x.TransactionId,
                                           PaymentDate = x.TransactionDate,
                                           Invoice = new dto_Invoice()
                                           {
                                               CustomerName = x.Name,
                                               InvoiceNumber = x.ReferenceNumber,
                                               TransactionId = x.TransactionId
                                           }
                                       });

                        var bf = _financeRepository.GetCashBookBBF(transaction.TransactionDate, transaction.LocationId);
                        var totalExpense = expense.FirstOrDefault()==null? 0 : expense.Sum(x => x.Amount);
                        var totalPayment = payment.FirstOrDefault()==null? 0 : payment.Sum(x => x.Amount);
                        var cb = (bf + totalPayment) - totalExpense;

                        lstCashBook.Add(
                            new dto_CashBook()
                            {
                                BalanceBF = bf,
                                TotalExpense = totalExpense,
                                TotalPayment = totalPayment,
                                BalanceCD = cb,
                                TransactionStartDate = (DateTime)fromDate,
                                TransactionEndDate = (DateTime)toDate,
                                ReportDate = transaction.TransactionDate,
                                Expense = expense.ToList(),
                                Payment = payment.ToList()
                            });
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }




                }

                lstCashBookList.Add(new dto_CashBookList()
                {
                    LocationName =location.LocationName,
                    CashBook = lstCashBook
                });



            }



            return lstCashBookList.ToList();



        }
        catch (Exception ex)
        {
            throw ex;
        }


    }

我想知道是否有更好的方法可以编写此代码。与使用stored procedure linq相比,我更愿意使用query实现这一目标。

0 个答案:

没有答案