English 中文(简体)
如何计算除第一个项目价值外的所有价值总额?
原标题:How to calculate the sum of all values in a dictionary excluding the first item s value?

我有一条从第二个项目开始计算所有价值(dec价值)的总和的字典。 是否可使用LINQ?

最佳回答

采用LINQ可实现:

myDict.Skip(1).Sum(x => x.Value);

However, the standard Dictionary class doesn t guarantee ordering of items, so the "first" item can be anything.

问题回答

为什么不仅仅把他们归到一起,然后把第一个项目抛开?

myList.Sum(x => x.Value) - myList.First().Value;

因此,第一点是空洞的;

dict.Skip(1).Sum(ix => ix.Value);

• 便于使用准则;

var dict = new Dictionary<string, decimal>();

dict.Add("A", 1.5m); // This value will be skipped
dict.Add("B", 2.7m);
dict.Add("C", 1.3m);
dict.Add("D", 3.9m);

var total = dict.Skip(1).Sum(v => v.Value);

Console.WriteLine(total);

2. 选择第一个项目的关键,将其从中排除:

dict.Sum(T => T.Key == "FirstKey" ? 0 : T.Value);

收取<条码>电视 议程项目

total = d.Skip(0).Sum(x => x.Value) 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签