2016-09-26 98 views
3

我有兩個過濾器。一種是按月份,年份,日期分組,另一種過濾器是選擇是否有人想按產品或公司分組。還可以有更多的過濾器。現在在一開始我寫了如果其他條件如下。Linq中基於過濾器的動態分組

List<IGrouping<String, TopItemReportData>> GroupedData = new List<IGrouping<String, TopItemReportData>>(); 

if (filters.DateBy.ToLower().Replace(" ", "") == "yearlyreport") 
    GroupedData = topsalesQueryable.GroupBy(x => x.SalesDate.Year.ToString()).OrderByDescending(x => x.Sum(y => y.SellingQuantity)).Skip((filters.CurrentPage - 1) * filters.RecordsPerPage).Take(filters.RecordsPerPage).ToList(); 
else if (filters.DateBy.ToLower().Replace(" ", "") == "monthlyreport") 
    GroupedData = topsalesQueryable.GroupBy(x => x.SalesDate.Month.ToString()).OrderByDescending(x => x.Sum(y => y.SellingQuantity)).Skip((filters.CurrentPage - 1) * filters.RecordsPerPage).Take(filters.RecordsPerPage).ToList(); 
else 
    GroupedData = topsalesQueryable.GroupBy(x => x.SalesDate.Date.ToString()).OrderByDescending(x => x.Sum(y => y.SellingQuantity)).Skip((filters.CurrentPage - 1) * filters.RecordsPerPage).Take(filters.RecordsPerPage).ToList(); 

if (filters.GroupBy.ToLower().Replace(" ", "") == "variantoption") 
    GroupedData = topsalesQueryable.GroupBy(x => (x.VariantId).ToString()).OrderByDescending(x => x.Sum(y => y.SellingQuantity)).Skip((filters.CurrentPage - 1) * filters.RecordsPerPage).Take(filters.RecordsPerPage).ToList(); 
else if (filters.GroupBy.ToLower() == "product") 
    GroupedData = topsalesQueryable.GroupBy(x => (x.ProductId).ToString()).OrderByDescending(x => x.Sum(y => y.SellingQuantity)).Skip((filters.CurrentPage - 1) * filters.RecordsPerPage).Take(filters.RecordsPerPage).ToList(); 

但是,這不會工作,因爲現在我必須根據選定的過濾器值同時分兩年和產品。所以,我不知道什麼被作爲編譯器給錯誤替換字符串時,我嘗試這樣做:

GroupedData = topsalesQueryable.GroupBy(x => new {x.SalesDate.Year.ToString(),x.ProductId.ToString()}).OrderByDescending(x => x.Sum(y => y.SellingQuantity)).Skip((filters.CurrentPage - 1) * filters.RecordsPerPage).Take(filters.RecordsPerPage).ToList(); 

匿名類型的成員必須有一個成員分配聲明。我的問題是如何擺脫如果其他以及如何解決這個錯誤

回答

0

你想要的只是一個單一的字符串與年份和產品ID。

我們有一個非常簡單的方法來創建:

string.Format("{0:0000}|{1:000000}", x.SalesDate.Year, x.ProductId) 

冒號後的零點得到字段的大小,萬一產品編號長短不一。

整個事情(清理了一下)是:

void Main() 
{ 
    var filters = new Filters(); 
    var topsalesQueryable = new List<TopItemReportData>(); 
    var TopItemReportData = new List<TopItemReportData>(); 
    IEnumerable<IGrouping<String, TopItemReportData>> tempGroupedData; 

    var dateBy = filters.DateBy.ToLower().Replace(" ", ""); 
    var groupBy = filters.GroupBy.ToLower().Replace(" ", ""); 


    if (dateBy == "yearlyreport") 
    { 
     if (groupBy == "product") 
      tempGroupedData = TopItemReportData 
           .GroupBy(x =>string.Format("{0:0000}|{1:000000}", 
             x.SalesDate.Year, x.ProductId)); 
     else 
      tempGroupedData = TopItemReportData.GroupBy(x => x.SalesDate.Year.ToString()); 
    } 
    else if (dateBy == "monthlyreport") 
     tempGroupedData = topsalesQueryable.GroupBy(x => x.SalesDate.Month.ToString()); 
    else 
     tempGroupedData = topsalesQueryable.GroupBy(x => x.SalesDate.Date.ToString()); 

    if (groupBy == "variantoption") 
     tempGroupedData = topsalesQueryable.GroupBy(x => x.VariantId.ToString()); 
    else if (groupBy == "product") 
     tempGroupedData = topsalesQueryable.GroupBy(x => x.ProductId.ToString()); 

    var GroupedData = tempGroupedData 
          .OrderByDescending(x => x.Sum(y => y.SellingQuantity)) 
          .Skip((filters.CurrentPage - 1) * filters.RecordsPerPage) 
          .Take(filters.RecordsPerPage) 
          .ToList(); 
} 

class Filters 
{ 
    public string DateBy; 
    public string GroupBy; 
    public int CurrentPage; 
    public int RecordsPerPage; 

    public Filters() 
    { 
     DateBy = "Yearly Report"; 
     GroupBy = "variantoption"; 
    } 

} 

class TopItemReportData 
{ 
    public DateTime SalesDate; 
    public int SellingQuantity; 
    public int VariantId; 
    public int ProductId; 
} 

您仍然需要理順如果/別人的,但我認爲,以消除DateBy財產的最好方式,只是使用GroupBy。

+0

我懷疑Entity Framework會喜歡'string.Format' :) –

+0

@IvanStoev是對的。 string.Format不適用於linq –

+0

@SafiMustafa好的,它不是那麼優雅,只是試着'x.SalesDate.Year +「|」 + x.ProductId' –