gpt4 book ai didi

c# - Linq中基于过滤器的动态分组

转载 作者:行者123 更新时间:2023-11-30 18:19:07 24 4
gpt4 key购买 nike

我有两个过滤器。一个是按月、年、日分组,另一个过滤器是选择是否有人想按产品或公司分组。也可以有更多的过滤器。现在一开始我写了 if else 用于特定条件,如下所示。

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

该匿名类型成员必须使用成员赋值声明。我的问题是如何摆脱 if else 以及如何解决这个错误

最佳答案

您需要的只是一个包含年份和产品 ID 的字符串。

我们有一个非常简单的方法来创建它:

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

冒号后的零表示字段的大小,以防 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;
}

您仍然需要理顺 if/elses,但我认为最好的方法是消除 DateBy 属性并只使用 GroupBy。

关于c# - Linq中基于过滤器的动态分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39706229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com