简体   繁体   English

使用 linq C# 返回列表值

[英]Returning List value using linq C#

Actually I want to return the data from different lists based on Date.其实我想根据日期从不同的列表中返回数据。 When i'm using this i'm getting data upto @Var result but i'm unnable to return the data.当我使用它时,我将数据提高到 @Var 结果,但我无法返回数据。 The issue with this is i'm getting error @return result.这个问题是我收到错误@return 结果。 I want to return the data @return result.我想返回数据@return 结果。 I'm using Linq C#.我正在使用 Linq C#。 Can anyone help me out?谁能帮我吗?

    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;**

    }

You can either convert your result to a List before returning it using return result.ToList() or make your method return an IEnumerable<CustomerWiseMonthlySalesReportDetails> instead of List .您可以在使用return result.ToList()之前将result转换为List ,或者使您的方法返回IEnumerable<CustomerWiseMonthlySalesReportDetails>而不是List

As your result is an enumeration of anonymous types you have to convert them to your CustomerWiseMonthlySalesReportDetails -type first:由于您的结果是匿名类型的枚举,您必须首先将它们转换为CustomerWiseMonthlySalesReportDetails类型:

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

Assuming your type has exactly the members returned by the select.假设您的类型完全具有选择返回的成员。

EDIT: So your code should look like this:编辑:所以你的代码应该是这样的:

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();

You can assume Alldates property if is date of one of groups that month of date is in right place:您可以假设 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM