//1.from 子句: int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var query = from n in arr where n > 3 select n; foreach (var i in query) { Console.WriteLine("" + i); } //2.select 子句: int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var query = from n in arr select n * 2; foreach (var i in query) { Console.WriteLine("" + i); } //3.where 子句: int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var query = from n in arr where test(n) > 5 select n; foreach (var i in query) { Console.WriteLine(i); } //4.let 子句: int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var query = from n in arr let num = n * 2 where num > 5 select n; foreach (var i in query) { Console.WriteLine(i); } //5.order by 子句: int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var query = from n in arr where n > 3 orderby n descending select n; foreach (var i in query) { Console.WriteLine(i); } //6.group by 子句: int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var query = from n in arr where n > 1 group n by n % 2; foreach (var i in query) { foreach (var j in i) { Console.WriteLine(j); } } //7.into 子句: int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var query = from n in arr where n > 1 group n by n % 2 into temp from sn in temp select sn; foreach (var i in query) { Console.WriteLine(i); } //8.join 子句: int[] arrA = { 0, 1, 2, 4, 5, 6, 7, 8, 9 }; int[] arrB = { 0, 3, 5, 6, 9 }; var query = from n in arrA join ni in arrB on n equals ni select ni; foreach (var i in query) { Console.WriteLine(i); }
/* * 1.协变与异变: * 协变:指能够使用与原始指定的派生类型相比,派生程度更大的类型 * string-->object * 逆变:指能够使用派生程度更新的类型 * object-->string * */ string[] arr = { "C#", "java", "javascript", "VC", "VB", "VB.net", "C++" }; var query = from n in arr group n by n.Length into lentGroup orderby lentGroup.Key //协变与逆变 select lentGroup; foreach (var i in query) { Console.WriteLine("" + i.Key); foreach (var str in i) { Console.WriteLine("" + str); } }