1.8. fejezet, LINQ
Beküldte pzoli - 2013, június 12 - 8:41du
namespace LINQProject { class Program { static void Main(string[] args) { List<int> list = new List<int>() { 10, 2, 10, 2, 4, 55, 22, 75, 30, 11, 12, 89 }; var result = (from number in list select number).Distinct().Where(x => x > 10).OrderBy(x => x); int idx = 0; Console.WriteLine("\nDistinct gt 10:"); foreach (int i in result) { Console.WriteLine("{0}. {1}", idx, i); idx++; } Console.ReadKey(); var result0 = from number in list orderby number descending select number; idx = 0; Console.WriteLine("\nOrder by number desc:"); foreach (int i in result0) { Console.WriteLine("{0}. {1}", idx, i); idx++; } Console.ReadKey(); List<string> names = new List<string>() { "Szandra", "István", "Iván", "Jolán", "Jenő", "Béla", "Balázs", "Viktória", "Vazul", "Töhötöm", "Tamás" }; var result1 = names.OrderByDescending(name => name[0]).ThenByDescending(name => name[1]); Console.WriteLine("\nName list order by name desc:"); foreach (var item in result1) { Console.WriteLine(item); } Console.ReadKey(); var result2 = from name in names orderby name[0], name[1] select name; Console.WriteLine("\nName list order by name asc:"); foreach (var item in result2) { Console.WriteLine(item); } Console.ReadKey(); var result3 = from name in names orderby name[0] group name by name[0] into namegroup select namegroup; Console.WriteLine("\nName list group by letter:"); foreach (var group in result3) { Console.WriteLine(group.Key); foreach (var name in group) { Console.WriteLine("-- {0}", name); } } Console.ReadKey(); names.Add("Péter"); names.Add(null); var result32 = from name in names group name by name == null ? '0' : name[0] into namegroup select namegroup; Console.WriteLine("\nName list group by letter:"); foreach (var group in result32) { Console.WriteLine(group.Key); foreach (var name in group) { Console.WriteLine("-- {0}", name); } } Console.ReadKey(); List<int> list2 = new List<int>() { 54, 12, 44, 67, 87, 3, 54, 11, 89 }; Console.WriteLine("\nList1:"); foreach (int i in list) { Console.Write("{0} ", i); } Console.WriteLine("\nList2:"); foreach (int i in list2) { Console.Write("{0} ", i); } Console.WriteLine("\nConcat:"); var concat= list.Concat(list2); foreach (int i in concat) { Console.Write("{0} ", i); } Console.ReadKey(); Console.WriteLine("\nUnion:"); var uni = list.Union(list2); foreach (int i in uni) { Console.Write("{0} ", i); } Console.ReadKey(); Console.WriteLine("\nIntersect:"); var intsect = list.Intersect(list2); foreach (int i in intsect) { Console.Write("{0} ", i); } Console.ReadKey(); Console.WriteLine("\nExcept:"); var except = list.Except(list2); foreach (int i in except) { Console.Write("{0} ", i); } Console.ReadKey(); Console.WriteLine("\nAverage: {0}, Max: {1}, Min: {2}, SQRSum {3}:", list.Average(), list.Max(), list.Min(), list.Sum((item) => item*item)); Console.ReadKey(); var chars = new[] { "a", "b", "c", "d" }; var csv = chars.Aggregate((a, b) => a + ',' + b); Console.WriteLine("\ncsv: {0}", csv); Console.ReadKey(); var csv2 = chars.Aggregate(new StringBuilder(), (a, b) => { if (a.Length > 0) a.Append(","); a.Append(b); return a; }); Console.WriteLine("\ncsv2: {0}", csv2); Console.ReadKey(); int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int aggregatedValue = numbers.Aggregate((total, nextValue) => total + nextValue); Console.WriteLine("numbers sum: {0}", aggregatedValue); Console.ReadKey(); Console.WriteLine("\nParallel list"); var result4 = from x in list.AsParallel() orderby x select x; foreach (int i in result4) { Console.Write("{0} ", i); } Console.ReadKey(); Console.WriteLine("\nToList"); var result5 = (from number in result4 where number > 5 select number).ToList<int>(); result5.ForEach((number) => Console.WriteLine(number)); Console.ReadKey(); ArrayList listAlpha = new ArrayList(); listAlpha.Add("alpha"); listAlpha.Add("beta"); listAlpha.Add(12345); var cast1 = from item in listAlpha.OfType<string>() select item; Console.WriteLine("\nOfType:"); foreach (var item in cast1) { Console.WriteLine(item); } Console.ReadKey(); var cast2 = from item in listAlpha.Cast<string>() select item; Console.WriteLine("\nCast:"); try { foreach (var item in cast2) { Console.WriteLine(item); //exception } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); } } }
- A hozzászóláshoz be kell jelentkezni