Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Fórum SO ➡ Merging 2 lists by Name and adding their Values, using LINQ ♦
//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 SHOW_RESULTS using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; // aljodav // http://rextester.com/users/376 // related to a thread in SO // http://bit.ly/2fghWWo namespace Rextester { using LO=List<Rextester.Object>; using Ob=Rextester.Object; class Object{ public string Name; public int Value; } class Program { static LO GetList1(){ return new LO{ new Ob{Name="Object1",Value=1}, new Ob{Name="Object2",Value=2}, new Ob{Name="Object3",Value=3}, new Ob{Name="Object4",Value=4}, new Ob{Name="Object5",Value=5}, }; } static LO GetList2(){ return new LO{ new Ob{Name="Object5",Value=10}, new Ob{Name="Object2",Value=20}, new Ob{Name="Object1",Value=30}, }; } public static void Main(string[] args) { LO newLO=GetList1() .GroupJoin(GetList2(), ob1=>ob1.Name, ob2=>ob2.Name, (ob1,ob2s)=>new Ob{ Name=ob1.Name, Value=ob1.Value+ob2s.Sum(x=>x.Value) } ) .ToList() ; #if SHOW_RESULTS Console.WriteLine("The type:\n\n{0}\n\n\t\tName\t\tValue",newLO.GetType()); newLO.ForEach(x=>Console.WriteLine("\t\t{0}\t\t{1}",x.Name,x.Value)); #endif Console.WriteLine("\n\nHello, world!"); } } }
run
|
edit
|
history
|
help
0
dsfsdfdf
hello-shared
Compare 2 int
float to short cast overflow
world
Math Operations
Pizzas & List of Tuples
mendozaeject
Const
compiled prog