Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何按产品类型和月份显示产品计数分组?_C#_Linq - Fatal编程技术网

C# 如何按产品类型和月份显示产品计数分组?

C# 如何按产品类型和月份显示产品计数分组?,c#,linq,C#,Linq,我有一个包含4列的表,例如ProductName、ProductType、ProductProductionStartDate、ProductProductionCompleteDate 我需要做的是显示12个月内每种产品类型的产品数量 我该怎么做 下面是我的代码: var model = context.Projects .Where(p => (p.ProductProductionStartDate > sdt && p.ProductProductio

我有一个包含4列的表,例如ProductName、ProductType、ProductProductionStartDate、ProductProductionCompleteDate

我需要做的是显示12个月内每种产品类型的产品数量

我该怎么做

下面是我的代码:

var model = context.Projects
    .Where(p => (p.ProductProductionStartDate > sdt && p.ProductProductionStartDate <= edt) && (p.ProductProductionCompleteDate != null))
    .GroupBy(p => new
    {
        Month = p.ProductProductionStartDate.HasValue ? p.ProductProductionStartDate.Value.Month : 0,
        Year = p.ProductProductionStartDate.HasValue ? p.ProductProductionStartDate.Value.Year : 0,
        productType = p.ProductType
    })
   .Select(g => new Dashmain
   {
       ProductType = g.Key.StoreType,
       Month = g.Key.Month,
       Year = g.Key.Year,
       Total = g.Count()
   })
   .OrderByDescending(a => a.Year)
   .ThenBy(a => a.Month)
   .ToList();
var model=context.Projects
.其中(p=>(p.ProductionStartDate>sdt&&p.ProductionStartDate新建
{
月=p.ProductionStartDate.HasValue?p.ProductionStartDate.Value.Month:0,
年份=p.ProductionStartDate.HasValue?p.ProductionStartDate.Value.Year:0,
productType=p.productType
})
.选择(g=>new Dashmain
{
ProductType=g.Key.StoreType,
月份=g.Key.Month,
年份=g.Key.Year,
总计=总计数()
})
.OrderByDescending(a=>a.Year)
.ThenBy(a=>a.Month)
.ToList();

我想用这种方式显示数据
{[Feb,10,20,5],[March,10,20,5]}

您需要在类型上嵌套
GroupBy
,如下所示:

var model = context.Projects
    .Where(p => (p.ProductProductionStartDate > sdt && p.ProductProductionStartDate <= edt) && (p.ProductProductionCompleteDate != null))
    .GroupBy(p => new
    {
        Month = p.ProductProductionStartDate.HasValue ? p.ProductProductionStartDate.Value.Month : 0,
        Year = p.ProductProductionStartDate.HasValue ? p.ProductProductionStartDate.Value.Year : 0
    })
   .Select(g => new Dashmain
   {
       Month = g.Key.Month,
       Year = g.Key.Year,
       Totals = g.GroupBy(gg => gg.StoreType).OrderBy(gg => gg.Key).Select(gg => gg.Count()).ToList()
   })
   .OrderByDescending(a => a.Year)
   .ThenBy(a => a.Month)
   .ToList();
var model=context.Projects
.其中(p=>(p.ProductionStartDate>sdt&&p.ProductionStartDate新建
{
月=p.ProductionStartDate.HasValue?p.ProductionStartDate.Value.Month:0,
年份=p.ProductionStartDate.HasValue?p.ProductionStartDate.Value.Year:0
})
.选择(g=>new Dashmain
{
月份=g.Key.Month,
年份=g.Key.Year,
Totals=g.GroupBy(gg=>gg.StoreType).OrderBy(gg=>gg.Key).选择(gg=>gg.Count()).ToList()
})
.OrderByDescending(a=>a.Year)
.ThenBy(a=>a.Month)
.ToList();

请注意,这种方法有一个限制:某个类别的产品为零的月份将完全没有数据。要解决此问题,您可以构建一个帮助器方法,为缺少类型的产品添加零。

能否显示输入和输出的示例?它不会抛出错误。它可以工作,但我不希望这样做调整仪表板表中的数据大小。它以如下方式返回列表{[feb,10],[feb,20],[feb,5]},{[March,10],[March,20],[March,5]}。我想以这种方式显示数据{[feb,10,20,5],[March,10,20,5]}示例中的数字是每个月按类型划分的产品计数