标签:
class Program{static void Main(string[] args){int[] array = {1,3,5,7,9,2,4,6,8,0 };var linq = from i in array where i < 5 orderby i select i;foreach (int i in linq){Console.WriteLine(i);//输出:0 1 2 3 4}}}
class People{public string Name { set; get; }public int Age { set; get; }public override string ToString(){return Name + " " + Age;}}class Program{static void Main(string[] args){People[] ps = new[] { new People { Name = "李志伟1", Age = 1 } ,new People { Name = "李志伟3", Age = 3 } ,new People { Name = "李志伟5", Age = 5 } ,new People { Name = "李志伟7", Age = 7 } ,new People { Name = "李志伟9", Age = 9 } ,new People { Name = "李志伟11", Age = 11 }};var linq = from p in ps where p.Age < 7 orderby p.Age descending select new People { Name =p.Name,Age=p.Age+10};foreach (People p in linq){Console.WriteLine(p);//输出的Age分别是15 13 11}Console.Read();}}
class Program{static void Main(string[] args){int[] array = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0, 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };var linq = from i in array where i > 5 orderby i select i;Console.WriteLine(linq.Count<int>());//输出8(array的元素个数)Console.WriteLine(linq.Max<int>());//输出9(array中最大的数)//输出8(array转换成List集合的元素个数)Console.WriteLine(linq.ToList<int>().Count);//输出4(array中除去重复的元素后元素个数)Console.WriteLine(linq.Distinct<int>().Count<int>());}}
class Program{static void Main(string[] args){int[] array1 = { 1, 3, 5, 7, 9 };int[] array2 = { 0, 2, 4, 6, 8 };var linq = from i in array1 from j in array2 where i < 5 select i+j;foreach (int i in linq){Console.WriteLine(i);//输出:1 3 5 7 9 3 5 7 9 11}}}
class People{public string Name { set; get; }public int Age { set; get; }public override string ToString(){return Name + " " + Age;}}class Program{static void Main(string[] args){People[] ps = new[] { new People { Name = "李志伟1", Age = 1 } ,new People { Name = "李志伟3", Age = 3 } ,new People { Name = "李志伟5", Age = 5 } ,new People { Name = "李志伟7", Age = 7 } ,new People { Name = "李志伟9", Age = 9 }};int[] array = { 1, 2, 3, 4, 5, 6 };var linq = from i in array join p in ps on i equals p.Age select p.Name;foreach (string name in linq){Console.WriteLine(name);//输出:李志伟1 李志伟3 李志伟5}Console.Read();}}
class Program{static void Main(string[] args){string[] words = { "apples", "blueberries", "oranges", "bananas", "apricots" };var wordGroups1 = from w in words group w by w[0];foreach (var item in wordGroups1){Console.WriteLine(item.Key + "-->" + item.Count<string>());//a-->2 b-->2 o-->1}}}
class Program{static void Main(string[] args){string[] words = { "apples", "blueberries", "oranges", "bananas", "apricots" };var wordGroups1 = from w in wordsgroup w by w[0] into fruitGroupwhere fruitGroup.Count() >= 2select new { FirstLetter = fruitGroup.Key, Words = fruitGroup.Count() };foreach (var item in wordGroups1){Console.WriteLine("以{0}开头的有{1}个", item.FirstLetter, item.Words);}}}

标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4315965.html