C# 如何使用LINQ求和?

C# 如何使用LINQ求和?,c#,linq,C#,Linq,我有这样的结构: private readonly Dictionary<string, Dictionary<string, int>> _storage = new Dictionary<string, Dictionary<string, int>>(); 专用只读字典\u存储= 新字典(); 键:固件(字符串):键:设备(字符串):值用户数(int) 我需要得到每个设备的用户总数,但我真的不知道如何使用LINQ。已经尝试了很多变

我有这样的结构:

private readonly Dictionary<string, Dictionary<string, int>> _storage =
    new Dictionary<string, Dictionary<string, int>>();
专用只读字典\u存储=
新字典();
键:固件(字符串):键:设备(字符串):值用户数(int)

我需要得到每个设备的用户总数,但我真的不知道如何使用LINQ。已经尝试了很多变体。求求你,救命

现在,我只使用一个完整的函数

private XlsRow2 GetTotalPerDevice(Dictionary<string, Dictionary<string, int>> storage)
    {
        XlsRow2 totalPerDeviceRow = new XlsRow2();
        totalPerDeviceRow._Name = "Grand Total";
        totalPerDeviceRow.UseBorders = true;
        foreach (var deviceModel in _allDeviceModels)
        {
            foreach (var firmware in storage)
            {
                foreach (var device in firmware.Value)
                {
                    var countOfUsers = 0;
                    if (deviceModel == device.Key)
                    {
                        countOfUsers += device.Value;

                        if (!_totalsPerDevice.ContainsKey(deviceModel))
                        {
                            _totalsPerDevice.Add(deviceModel, countOfUsers);
                        }
                        else
                        {
                            _totalsPerDevice[deviceModel] += countOfUsers;
                        }
                    }
                }
            }
        }
        foreach (var deviceModel in _allDeviceModels)
        {
            if (_totalsPerDevice.ContainsKey(deviceModel))
            {
                totalPerDeviceRow._AddColumn(_totalsPerDevice.First(k => k.Key == deviceModel.ToString()).Value.ToString());
            }
            else
            {
                totalPerDeviceRow._AddColumn("");
            }
        }
        return totalPerDeviceRow;
    }
private XlsRow2 GetTotalPerDevice(字典存储)
{
XlsRow2 totalPerDeviceRow=新的XlsRow2();
totalPerDeviceRow.\u Name=“总计”;
totalPerDeviceRow.UseBorders=true;
foreach(所有设备模型中的var设备模型)
{
foreach(存储中的var固件)
{
foreach(固件中的var设备。值)
{
var countofuser=0;
if(deviceModel==device.Key)
{
countOfUsers+=设备值;
如果(!\u totalsPerDevice.ContainsKey(设备模型))
{
_添加(设备模型,用户数);
}
其他的
{
_totalsPerDevice[设备模型]+=用户数;
}
}
}
}
}
foreach(所有设备模型中的var设备模型)
{
如果(_totalsPerDevice.ContainsKey(设备模型))
{
totalPerDeviceRow.\u AddColumn(\u totalsPerDevice.First(k=>k.Key==deviceModel.ToString()).Value.ToString());
}
其他的
{
totalPerDeviceRow.\u AddColumn(“”);
}
}
返回totalPerDeviceRow;
}

像这样的例子

var result = _storage.SelectMany(x => x.Value)
    .GroupBy(x => x.Key)
    .Select(x => new { Device = x.Key, Total = x.Sum(y => y.Value) });

由于要聚合的数据的键位于第二级字典中,因此最好的第一步是将内部字典中的所有键值对转储到一个平面序列中。在此之后,您只需汇总计数,如下所示:

var res = _storage
    .SelectMany(d => d.Value)
    .GroupBy(kvp => kvp.Key)
    .ToDictionary(g => g.Key, g => g.Sum(kvp => kvp.Value));

字典实现了
IEnumerable,您可以这样做:

Dictionary<string, Dictionary<string, int>> _storage = new Dictionary<string, Dictionary<string, int>>();
Dictionary<string, int> x = new Dictionary<string, int>();
x.Add("x", 2);
x.Add("z", 2);
x.Add("y", 2);
_storage.Add("x", x);
_storage.Add("z", x);
_storage.Add("y", x);

 var b = _storage.SelectMany(keyValuePair => keyValuePair.Value)
         .GroupBy(keyValuePair => keyValuePair.Key)
         .ToDictionary(valuePairs => valuePairs.Key, grouping => grouping.Sum(kvp => kvp.Value));
Dictionary\u storage=newdictionary();
字典x=新字典();
x、 添加(“x”,2);
x、 添加(“z”,2);
x、 添加(“y”,2);
_存储。添加(“x”,x);
_存储。添加(“z”,x);
_存储。添加(“y”,x);
var b=\u storage.SelectMany(keyValuePair=>keyValuePair.Value)
.GroupBy(keyValuePair=>keyValuePair.Key)
.ToDictionary(valuePairs=>valuePairs.Key,grouping=>grouping.Sum(kvp=>kvp.Value));
结果如下:

再次提醒大家,并非所有的最佳答案都有最高的分数,而只是那些已经有很多选票的答案。很抱歉兄弟。
_storage.Selectmany(pair=>pair.Value)
        .GroupBy(leaf=>leaf.Key);
var totals=_storage.SelectMany(pair=>pair.Value)
                   .GroupBy(leaf=>leaf.Key)
                   .Select(grp=>new {
                                        Device = grp.Key,
                                        TotalUsers =grp.Sum(leaf=>leaf.Value)
                                    });
var totals2 = from frm in _storage
              from dev in frm.Value
              group dev by dev.Key into grp
              select new {
                           Device = grp.Key,
                           Total=grp.Sum(leaf=>leaf.Value)
                         };
var _storage = new Dictionary<string, Dictionary<string, int>> {
    ["Frm1"]=new Dictionary<string, int> { 
                    ["Device1"]=4,
                    ["Device2"]=5
                },
    ["Frm2"]=new Dictionary<string, int> { 
                    ["Device1"]=41,
                    ["Device3"]=5
                }                    
};
foreach(var total in totals)
{
   Console.WriteLine ($"{total.Device} = {total.Total}");
}
------------------
Device1 = 45
Device2 = 5
Device3 = 5
Dictionary<string, Dictionary<string, int>> _storage = new Dictionary<string, Dictionary<string, int>>();
Dictionary<string, int> x = new Dictionary<string, int>();
x.Add("x", 2);
x.Add("z", 2);
x.Add("y", 2);
_storage.Add("x", x);
_storage.Add("z", x);
_storage.Add("y", x);

 var b = _storage.SelectMany(keyValuePair => keyValuePair.Value)
         .GroupBy(keyValuePair => keyValuePair.Key)
         .ToDictionary(valuePairs => valuePairs.Key, grouping => grouping.Sum(kvp => kvp.Value));