Tôi có cấu trúc này:Làm thế nào để sử dụng LINQ để tìm tổng?
private readonly Dictionary<string, Dictionary<string, int>> _storage =
new Dictionary<string, Dictionary<string, int>>();
chính: Firmware (string): Đây chính: Device (string): CountOfUsers giá trị (int)
tôi cần phải nhận được tổng số người sử dụng cho từng thiết bị, nhưng Tôi thực sự không biết làm thế nào để làm điều đó với LINQ. Đã thử rất nhiều biến thể. Hãy giúp tôi!
Còn bây giờ, tôi chỉ sử dụng một chức năng toàn bộ cho nó
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;
}
Một lần nữa, lời khuyên không phải là allways câu trả lời hay nhất có điểm số cao nhất nhưng chỉ đơn giản là một trong số đó đã có nhiều phiếu bầu. Lấy làm tiếc. bro. – HimBromBeere