标签:des blog http 使用 strong ar for 数据 art
什么是LINQ
匿名类型
第一个成员初始化器是赋值形式,第二个是成员访问表达式,第三个是标示符形式,如下实例所示:
1 string Sex = "男"; 2 var person = new { Age = 24, SimpleClass.Name, Sex }; 3 Console.WriteLine("{0},Sex{1},Age {2}",person.Name,person.Sex,person.Age);
运行结果:
注意标示符形式和访问表达式必须定义在匿名类型申明之前,Sex是一个局部变量,Name是类SimpleClass的静态字段。
尽管在代码中看不到匿名类型,对象浏览器却能看到。如果编译器遇到了另一个具有相同参数名、相同引用类型名和相同顺序的匿名类型,它会重用这个类型并直接创建新的实例,而不会创建新的匿名类型。
查询语法和方法语法
示例代码如下所示:
1 var query1 = from n in Enumerable.Range(1, 10) 2 where n < 8 3 select n; 4 Console.Write("查询语法得到结果:"); 5 foreach (var v in query1) Console.Write("{0}\t",v); 6 7 var query2 = Enumerable.Range(1, 8).Where(n => n < 8); 8 Console.WriteLine(""); 9 Console.Write("方法语法得到结果:"); 10 foreach (var v in query2) Console.Write("{0}\t", v); 11 12 int count = (from n in Enumerable.Range(1, 10) 13 where n < 8 14 select n).Count(); 15 Console.WriteLine(""); 16 Console.WriteLine("两种方式的组合:{0}", count);
运行结果:
查询表达式的结构
1、from子句
1 int[] arr = { 1, 5, 9, 8, 45, 23, 26, 14, 7, 8, 9 }; 2 var query = from n in arr //(n->迭代变量) 3 where n < 10 //使用迭代变量 4 select n; //使用迭代变量 5 foreach (var v in query) Console.Write("{0}\t",v); 6 Console.WriteLine("");
运行结果:
2、join子句
示例代码如下:
1 public class Student 2 { 3 public int StID; 4 public string LastName; 5 } 6 public class CourseStudent 7 { 8 public string CourseName; 9 public int StID; 10 } 11 12 CourseStudent[] studentInCourses = new CourseStudent[]{ 13 new CourseStudent{CourseName="Art",StID=1}, 14 new CourseStudent{CourseName="Art",StID=3}, 15 new CourseStudent{CourseName="History",StID=1}, 16 new CourseStudent{CourseName="History",StID=2}, 17 new CourseStudent{CourseName="Physics",StID=3} 18 }; 19 Student[] students = new Student[] { 20 new Student{StID=1,LastName="张三"}, 21 new Student{StID=2,LastName="李四"}, 22 new Student{StID=3,LastName="王五"} 23 }; 24 25 //获取选修2门课以上同学的名字 26 var query = (from s in students 27 join c in studentInCourses on s.StID equals c.StID 28 where (from x in studentInCourses where x.StID==c.StID select x.StID).Count()>1 29 select s.LastName).Distinct(); 30 foreach (var v in query) Console.Write("{0}\t",v); 31 Console.WriteLine(""); 32 //获取选修历史同学的名字 33 var query1 = from s in students 34 join c in studentInCourses on s.StID equals c.StID 35 where c.CourseName == "History" 36 select s.LastName; 37 foreach (var v in query1) Console.Write("{0}\t", v);
运行结果:
查询主体中的from...let...where片段
1、from子句
示例代码如下:
1 var someInts = from a in Enumerable.Range(1, 5) //必须的第一个from子句 2 from b in Enumerable.Range(6, 5) //查询主体的第一个子句 3 where a < 3 && b < 10 4 select new { a, b, sum = a + b }; //匿名类型对象 5 foreach (var v in someInts) Console.WriteLine(v);
2、let子句
示例代码如下:
1 var someInts = from a in Enumerable.Range(1, 5) 2 from b in Enumerable.Range(6, 5) 3 let sum = a + b //在新的变量中保存结果 4 where sum == 12 5 select new { a, b, sum }; 6 foreach (var v in someInts) Console.WriteLine(v);
运行结果:
3、where子句
示例代码如下:
1 var someInts = from a in Enumerable.Range(1, 5) 2 from b in Enumerable.Range(6, 5) 3 let sum = a + b 4 where sum > 12 //条件一 5 where a==4 //条件二 6 select new { a, b, sum }; 7 foreach (var v in someInts) Console.WriteLine(v);
运行结果:
orderby子句
示例代码如下:
1 var persons = new[] { //匿名类型的对象数组 2 new {Name="张三",Sex="男",Age=32,Address="广东深圳"}, 3 new {Name="李四",Sex="男",Age=26,Address="广东广州"}, 4 new {Name="王五",Sex="女",Age=22,Address="广东深圳"}, 5 new {Name="赵六",Sex="男",Age=33,Address="广东东莞"} 6 }; 7 var query = from p in persons 8 orderby p.Age 9 select p; 10 foreach (var p in query) Console.WriteLine("Name:{0},Sex:{1},Age:{2},Address:{3}", p.Name, p.Sex, p.Age, p.Address);
运行结果:
group by子句
示例代码如下:
1 var persons = new[] { //匿名类型的对象数组 2 new {Name="张三",Sex="男",Age=32,Address="广东深圳"}, 3 new {Name="李四",Sex="男",Age=26,Address="广东广州"}, 4 new {Name="王五",Sex="女",Age=22,Address="广东深圳"}, 5 new {Name="赵六",Sex="男",Age=33,Address="广东东莞"} 6 }; 7 var query = from p in persons 8 group p by p.Address; 9 foreach (var v in query) //枚举分组 10 { 11 Console.WriteLine("{0}",v.Key); //分组键 12 foreach (var t in v) //枚举分组中的项 13 { 14 Console.WriteLine("Name:{0},Sex:{1},Age:{2},Address:{3}", t.Name, t.Sex, t.Age, t.Address); 15 } 16 }
运行结果:
查询延续
示例代码如下:
1 var somInts = from a in Enumerable.Range(1, 10) 2 join b in Enumerable.Range(5, 10) on a equals b 3 into groupTemp //查询延续 4 from c in groupTemp 5 select c; 6 foreach (var v in somInts) Console.WriteLine(v);
运行结果如下:
使用委托参数和Lambda的示例
public static int Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
示例代码如下:
1 IList<int> list = Enumerable.Range(1, 100).ToList(); 2 Func<int, bool> myDel = delegate(int x) { return x % 2 == 1; };//委托匿名方法 3 var countOdd1 = list.Count(myDel);//调用委托 4 var countOdd2 = list.Count(x => x % 2 == 1);//Lambda表达式 5 Console.WriteLine("委托参数得到奇数的个数:{0}",countOdd1); 6 Console.WriteLine("Lambda得到奇数的个数:{0}", countOdd2);
运行结果:
【转载】http://www.cnblogs.com/cmsdn/archive/2012/04/12/2442107.html
标签:des blog http 使用 strong ar for 数据 art
原文地址:http://www.cnblogs.com/julin-peng/p/3946912.html