编写函数(方法)以打印出交易明细

时间:2019-06-24 08:50:29

标签: c# entity-framework linq

我已经使用linq查询显示了交易明细。现在如何编写以交易ID为参数的函数(或方法)以打印出交易明细?

            var context = new Context();
            var result = (from sale in context.PurchaseHeads
                     join line in context.PurchaseLines on sale equals line.PurchaseHead
                     join staff in context.Staffs on sale.Staff equals staff
                     join store in context.Stores on sale.Store equals store
                     join customer in context.Customers on sale.Customer equals customer
                     join product in context.Products on line.Product equals product
                     group new { line.Quantity, product.Price } by new
                     { sale.ID,
                         StaffName = staff.Name,
                         StoreName = store.Name,
                         CustomerName = customer.Name} into gr
                     select new
                     {
                         TransactionID = gr.Key.ID,
                         StaffName = gr.Key.StaffName,
                         StoreName = gr.Key.StoreName,
                         CustomerName = gr.Key.CustomerName,
                         Total = gr.Sum(x => x.Quantity * x.Price),
                     }).ToList();

 foreach (var item in result)
            {
                Console.WriteLine(item.TransactionID + " " + item.StaffName + " " + item.StoreName + " " + item.CustomerName + " " + item.Total);
            }
            Console.ReadLine();

2 个答案:

答案 0 :(得分:0)

将结果保存在Dictionary中而不是列表中可能不是一个坏主意,那么您可以 result[<ID>]获取相应的详细信息。

如果出于任何原因需要将其保留在列表中:

result.FirstOrDefault(x => x.TransactionID == <ID>)

如果没有具有给定id的条目,它将返回null。

答案 1 :(得分:0)

您可以在函数中传递结果

public void printTransactionDetails(List<ResultClass> result, int transactionId) {
     var transaction = result.FirstOrDefault(x => x.TransactionId == transactionId);

     if (transaction != null) {
         Console.WriteLine(transaction.TransactionID + " " + transaction.StaffName + " " + transaction.StoreName + " " + transaction.CustomerName + " " + item.Total);
     }
}

或在函数内部查询结果

public void printTransactionDetails(int transactionId) {
     var context = new Context();
        var result = (from sale in context.PurchaseHeads
                 join line in context.PurchaseLines on sale equals line.PurchaseHead
                 join staff in context.Staffs on sale.Staff equals staff
                 join store in context.Stores on sale.Store equals store
                 join customer in context.Customers on sale.Customer equals customer
                 join product in context.Products on line.Product equals product
                 group new { line.Quantity, product.Price } by new
                 { sale.ID,
                     StaffName = staff.Name,
                     StoreName = store.Name,
                     CustomerName = customer.Name} into gr
                 select new
                 {
                     TransactionID = gr.Key.ID,
                     StaffName = gr.Key.StaffName,
                     StoreName = gr.Key.StoreName,
                     CustomerName = gr.Key.CustomerName,
                     Total = gr.Sum(x => x.Quantity * x.Price),
                 }).FirstOrDefault(c => c.TransactionId == transactionId); 

     if (transaction != null) {
         Console.WriteLine(transaction.TransactionID + " " + transaction.StaffName + " " + transaction.StoreName + " " + transaction.CustomerName + " " + item.Total);
     }
}