首页 > 代码库 > LINQ to Object

LINQ to Object

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Collections;
 5 
 6 namespace Demo1
 7 {
 8     class Program
 9     {
10    
11         static void Main(string[] args)
12         {
13             Console.WriteLine("简单查询");
14             string[] currentVideoGames = { "Morrowind", "Uncharted 2", "Fallout  3", "Daxter", "System Shock 2" };
15             IEnumerable<string> subSet = from g in currentVideoGames where g.Contains(" ") orderby g select g;
16             foreach (string s in subSet)
17                 Console.WriteLine(s);
18 
19             int [] numbers = {10,20,30,1,2,4,8 };
20             List<Person> personList = new List<Person>
21             {
22                 new Person{Age = 15, Name = "Lucy"},
23                 new Person{Age = 13, Name = "Tom"},
24                 new Person{Age = 18, Name = "Bob"},
25                 new Person{Age = 20, Name = "Jake"}
26             };
27             Console.WriteLine("简单条件查询");
28             var resu = from i in numbers where i < 10 select i;
29             foreach (var s in resu)
30                 Console.WriteLine(s);
31             Console.WriteLine("复合条件查询");
32             var myPersons = from p in personList where p.Age >= 18 && p.Name!="Jake" select p;
33             foreach (var p in myPersons)
34                 Console.WriteLine("Age = {0}, Name = {1}",p.Age, p.Name);
35 
36             Console.WriteLine("把ArrayList转换成一个兼容于 IEnumerable<T>的类型");
37             ArrayList persons = new ArrayList()
38             {
39                 new Person{Age = 13, Name = "Tom" },
40                 new Person{Age = 20, Name = "Jake"}
41             };
42             var personsEnum = persons.OfType<Person>();
43             var personTom = from p in personsEnum where p.Name == "Tom" select p;
44             foreach (var p in personTom)
45                 Console.WriteLine("Age = {0}, Name = {1}",p.Age,p.Name);
46 
47             Console.WriteLine("使用 ofType<T> 筛选数据");
48             ArrayList myStuff = new ArrayList();
49             myStuff.AddRange(new object[]{10,400,8,false,new Person(), "string data"});
50             var myInts = myStuff.OfType<int>();
51             foreach (int i in myInts)
52                 Console.WriteLine("Int  value: {0}",i);
53 
54             Console.WriteLine("将查询结果投影到新数据类型");
55             var nameList = from n in personList select new { n.Name };
56             foreach (var n in nameList)
57                 Console.WriteLine(n.Name);
58             //    Except() 返回容器与比较的容器的不同之处
59             //    Intersect() 返回容器与比较的容器的相同之处
60             //    union() 合并成员,不允许重复(任何地方)
61             //    Concat()  直接连接,不检查(如果想检查,加上 Distinct() 即可)
62             //    Count(), Max(), Min(), Sum(), Average() ...s
63             Console.WriteLine("常用方法");
64             List<string> str1 = new List<string> { "Yugo", "Aztec", "BMW"};
65             List<string> str2 = new List<string>{"BMW","Saab","Aztec"};
66             Console.WriteLine("Use Except()");
67             foreach(var i in  str1.Except(str2))
68                 Console.WriteLine(i);
69             Console.WriteLine("Use Intersect()");
70             foreach (var i in str1.Intersect(str2))
71                 Console.WriteLine(i);
72             Console.WriteLine("Use Union()");
73             foreach (var i in str1.Union(str2))
74                 Console.WriteLine(i);
75             Console.WriteLine("Use Concat()");
76             foreach (var i in str1.Concat(str2))
77                 Console.WriteLine(i);
78             Console.ReadLine();
79         }
80     }
81     public class Person
82     {
83         public int Age { get; set; }
84         public string Name { get; set; }
85     }
86 }