使用linq C#返回List值

时间:2015-09-02 10:59:08

标签: c# linq

实际上我想根据日期返回不同列表中的数据。当我使用这个时,我将数据提升到@Var结果,但我无法返回数据。这个问题是我得到错误@return结果。我想返回数据@return结果。我正在使用Linq C#。任何人都可以帮助我吗?

    public List<CustomerWiseMonthlySalesReportDetails> GetAllCustomerWiseMonthlySalesReportCustomer()
    {
        var cbsalesreeport = (from cb in db.cashbilldescriptions
                              join c in db.cashbills on cb.CashbillId equals c.CashbillId
                              join p in db.products on cb.ProductId equals p.ProductId


                              select new
                              {
                                  Productamount = cb.Productamount,
                                  ProductName = p.ProductDescription,
                                  CashbillDate = c.Date
                              }).AsEnumerable().Select(x => new ASZ.AmoghGases.Model.CustomerWiseMonthlySalesReportDetails
                            {

                                Productdescription = x.ProductName,
                                Alldates = x.CashbillDate,
                                TotalAmount = x.Productamount
                            }).ToList();

        var invsalesreeport = (from inv in db.invoices
                               join invd in db.invoicedeliverychallans on inv.InvoiceId equals invd.InvoiceId
                               select new
                               {
                                   Productamount = invd.Total,
                                   ProductName = invd.Productdescription,
                                   InvoiceDate = inv.Date
                               }).AsEnumerable().Select(x => new ASZ.AmoghGases.Model.CustomerWiseMonthlySalesReportDetails
                            {

                                Productdescription = x.ProductName,
                                Alldates = x.InvoiceDate,
                                TotalAmount = x.Productamount
                            }).ToList();
        var abc = cbsalesreeport.Union(invsalesreeport).ToList();

        var result = (from i in abc
                 group i by new { Date = i.Alldates.ToString("MMM"), Product = i.Productdescription } into grp
                 select new { Month = grp.Key, Total = grp.Sum(i => i.TotalAmount) });

        **return result;**

    }

2 个答案:

答案 0 :(得分:0)

您可以将result转换为List,然后使用return result.ToList()将其返回,或者让您的方法返回IEnumerable<CustomerWiseMonthlySalesReportDetails>而不是List

由于您的结果是匿名类型的枚举,您必须将它们转换为CustomerWiseMonthlySalesReportDetails - 首先输入:

select new CustomerWiseMonthlySalesReportDetails{ Month = grp.Key, Total = grp.Sum(i => i.TotalAmount) });

假设您的类型完全具有select返回的成员。

编辑:所以你的代码应该是这样的:

var result = (from i in abc
             group i by new { Date = i.Alldates.ToString("MMM"), Product = i.Productdescription } into grp
             select new CustomerWiseMonthlySalesReportDetails{ Month = grp.Key, Total = grp.Sum(i => i.TotalAmount) });

return result.ToList();

答案 1 :(得分:0)

如果日期为正确的一个组的日期,您可以假设Alldates属性:

var result = (from i in abc
   group i by new { Date = i.Alldates.ToString("MMM"), Product = i.Productdescription } 
   into grp
   select new CustomerWiseMonthlySalesReportDetails{ 
          Productdescription = grp.Key.Product, 
          TotalAmount = grp.Sum(i => i.TotalAmount), 
          Alldates =grp.First(i=>i.Alldates ) })
   .ToList();