Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.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# Linq group by Sum,但如果Sum为null,则也返回0_C#_Linq_Group By - Fatal编程技术网

C# Linq group by Sum,但如果Sum为null,则也返回0

C# Linq group by Sum,但如果Sum为null,则也返回0,c#,linq,group-by,C#,Linq,Group By,我有以下类型: class ItemTotals { public decimal Total { get; set; } public string ItemType { get; set; } } 我使用以下方法获取每个项目类型的总计: var items = new List<ItemTotals> { new ItemTotals { Total

我有以下类型:

class ItemTotals
{
    public decimal Total { get; set; }
    public string ItemType { get; set; }
}
我使用以下方法获取每个项目类型的总计:

        var items = new List<ItemTotals>
        {
            new ItemTotals
            {
                Total = 10,
                ItemType = "X"
            },
            new ItemTotals
            {
                Total = 10,
                ItemType = "X"
            },
            new ItemTotals
            {
                Total = -5,
                ItemType = "Y"
            },
            new ItemTotals
            {
                Total = -20,
                ItemType = "Y"
            }
        };


        List<ItemTotals> query = items.GroupBy(x => x.ItemType)
                                      .Select(x => new ItemTotals
                                      {
                                          ItemType = x.Key,
                                          Total = x.Sum(z => z.Total)
                                      }).ToList();
var items=新列表
{
新项目总数
{
总计=10,
ItemType=“X”
},
新项目总数
{
总计=10,
ItemType=“X”
},
新项目总数
{
总数=-5,
ItemType=“Y”
},
新项目总数
{
总数=-20,
ItemType=“Y”
}
};
列表查询=items.GroupBy(x=>x.ItemType)
.选择(x=>new ItemTotals
{
ItemType=x.键,
总计=x.Sum(z=>z.Total)
}).ToList();
这给了我X的总数为20,Y的总数为-25。但是假设所有的项目类型都是X,那么对于X,我只得到一个结果,总计为-5,对于总计为0的Y,没有结果,这是可能的吗


谢谢

在列表初始化后添加这些行

items.Add(new ItemTotals {Total = 0, ItemType = "X"});
items.Add(new ItemTotals {Total = 0, ItemType = "Y"});

在列表初始化之后添加这些行

items.Add(new ItemTotals {Total = 0, ItemType = "X"});
items.Add(new ItemTotals {Total = 0, ItemType = "Y"});

您可以找到列表中未包含的类型,并将其添加到循环中:

List<string> itemTypes = new List<string>() {"X", "Y", "Z"};

foreach(var type in itemTypes)
{
     if(!items.Any(x => x.ItemType == type)
           items.Add(new ItemTotals() {ItemType = type, Total = 0})
}

List<ItemTotals> query = items.GroupBy(x => x.ItemType)....
List itemTypes=new List(){“X”、“Y”、“Z”};
foreach(itemTypes中的变量类型)
{
如果(!items.Any)(x=>x.ItemType==type)
添加(新的ItemTotals(){ItemType=type,Total=0})
}
列表查询=items.GroupBy(x=>x.ItemType)。。。。

您可以找到列表中未包含的类型,并将其添加到循环中:

List<string> itemTypes = new List<string>() {"X", "Y", "Z"};

foreach(var type in itemTypes)
{
     if(!items.Any(x => x.ItemType == type)
           items.Add(new ItemTotals() {ItemType = type, Total = 0})
}

List<ItemTotals> query = items.GroupBy(x => x.ItemType)....
List itemTypes=new List(){“X”、“Y”、“Z”};
foreach(itemTypes中的变量类型)
{
如果(!items.Any)(x=>x.ItemType==type)
添加(新的ItemTotals(){ItemType=type,Total=0})
}
列表查询=items.GroupBy(x=>x.ItemType)。。。。

一种方法是将已知名称与零配对的集合预先添加到集合中,如下所示:

var[] names = new {"X", "Y", "Z"};
List<ItemTotals> query = items
    .Concat(names.Select(n => new ItemTotals {Total = 0, ItemType = n}))
    .GroupBy(x => x.ItemType)
    ... // And so on
var[]name=new{“X”、“Y”、“Z”};
列表查询=项目
.Concat(names.Select(n=>newitemtotals{Total=0,ItemType=n}))
.GroupBy(x=>x.ItemType)
…等等

Concat
确保每个名称至少有一个项目,并且该项目的总数为零。在某种程度上,添加的项目充当“哨兵”:如果项目名称在那里,则它们没有区别,但如果名称不在那里,则它们确保将带零的行添加到结果中。

一种方法是将已知名称与零成对的集合预先添加到集合中,如下所示:

var[] names = new {"X", "Y", "Z"};
List<ItemTotals> query = items
    .Concat(names.Select(n => new ItemTotals {Total = 0, ItemType = n}))
    .GroupBy(x => x.ItemType)
    ... // And so on
var[]name=new{“X”、“Y”、“Z”};
列表查询=项目
.Concat(names.Select(n=>newitemtotals{Total=0,ItemType=n}))
.GroupBy(x=>x.ItemType)
…等等

Concat
确保每个名称至少有一个项目,并且该项目的总数为零。在某种程度上,添加的项目充当“哨兵”:如果项目名称在那里,它们没有区别,但是如果名称不在那里,它们会确保结果中添加一个带零的行。

如果列表中没有Y,那么算法如何根本不存在?如果列表中没有Y,那么算法如何根本不存在?