Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Fórum ➡ Calculate the Sum of Values in List of KeyValuePair<> objects ...
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 #define DISPLAY_THE_TABLE_ROWS using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; // aljodav // http://rextester.com/users/376 // in reply to the MSDN thread // http://bit.ly/2fGT2w5 namespace Rextester { using Data=KeyValuePair<string,int>; class Program { static List<Data> GetList(){ return new List<Data>{ new Data("Jim",100), new Data("Jim",200), new Data("Mae",567), new Data("Ana",31), new Data("Ana",13), new Data("Mae",210), new Data("Jim",700), }; } public static void Main(string[] args) { GetList() .GroupBy(kvp=>kvp.Key,kvp=>kvp.Value,(key,values)=>new{ Name=key, Total=values.Sum() }) .ToList() .ForEach(x=>Console.WriteLine("\t\t{0}\t{1}",x.Name,x.Total)) ; Console.WriteLine("\n\nHello, world!"); } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0,19 sec, absolute running time: 0,11 sec, cpu time: 0,11 sec, average memory usage: 14 Mb, average nr of threads: 3
edit mode
|
history
|
discussion
Jim 1000 Mae 777 Ana 44 Hello, world!