Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
linq subquery z grupowaniem po dwóch polach
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class stw { public string stawka {get;set;} public int kwota {get;set;} public dok dokument {get;set;} public stw(dok d) { dokument=d; } } public class dok { public int numer {get;set;} public string magazyn {get;set;} public List<stw> drabinka {get;set;} public dok() { drabinka = new List<stw>(); } } public class Program { public static void Main(string[] args) { List<dok> dokumenty = new List<dok>(); dok d; d = new dok(){numer=1,magazyn="A"}; d.drabinka.Add(new stw(d){stawka="23%",kwota=23}); dokumenty.Add(d); d = new dok(){numer=2,magazyn="A"}; d.drabinka.Add(new stw(d){stawka="12%",kwota=46}); dokumenty.Add(d); d = new dok(){numer=3,magazyn="A"}; d.drabinka.Add(new stw(d){stawka="8%",kwota=8}); dokumenty.Add(d); d = new dok(){numer=4,magazyn="B"}; d.drabinka.Add(new stw(d){stawka="23%",kwota=23}); d.drabinka.Add(new stw(d){stawka="8%",kwota=16}); dokumenty.Add(d); Console.WriteLine("dane:"); foreach(var q in dokumenty) { Console.WriteLine(q.numer + " - " + q.magazyn); foreach(var x in q.drabinka) Console.WriteLine(" "+x.stawka + " - " + x.kwota.ToString() +"("+x.dokument.numer.ToString()+")" ); } Console.WriteLine("query:"); var query = from dok in dokumenty from sta in dok.drabinka group sta.kwota by sta.stawka into kwoty select new { stawka = kwoty.Key, suma = kwoty.Sum() }; foreach(var q in query) Console.WriteLine(q.stawka + " - " + q.suma.ToString()); Console.WriteLine("query2:"); var query2 = dokumenty.SelectMany(x => x.drabinka).GroupBy(x => new {x.dokument.magazyn, x.stawka}).Select(x => new {stawkaImagazyn = x.Key, suma = x.Sum(y => y.kwota)}).ToList(); foreach(var q in query2) Console.WriteLine(q.stawkaImagazyn.magazyn + "," + q.stawkaImagazyn.stawka + " - " + q.suma.ToString()); //Console.WriteLine(q.stawka + " - " + q.kwota); //Console.WriteLine(q); } } }
run
|
edit
|
history
|
help
0
Basma 2
DecimalToStringConversion
type comparison
4
Student Management System
FIBNOCHI
Method overriding
Converting an int to an int[] using linq.
MockTest
Việc Làm Bắc Ninh