标签:
1 //这个代码声明了两个类 2 //第一个代表学生,每个学生都有学生的名字和ID 3 //第二个代表课程,每个课程类都有学生的ID和课程的名字 4 class Program 5 { 6 public class Student 7 { 8 public int StID; 9 public string LastName; 10 } 11 public class CourseStudent 12 { 13 public string CourseName; 14 public int StID; 15 } 16 //在全局中创建学生类与课程类 17 //课程类与学生类中有一样的ID 18 static Student[] students =newStudent[]{ 19 new Student{StID=1,LastName="Carson"}, 20 new Student{StID=2,LastName="Klassen"}, 21 new Student{StID=3,LastName="Fleming"}, 22 }; 23 static CourseStudent[] studentsInCourses =new CourseStuden t[]{ 24 new CourseStudent{CourseName="Art",StID=1}, 25 new CourseStudent{CourseName="Art",StID=2}, 26 new CourseStudent{CourseName="History",StID=1}, 27 new CourseStudent{CourseName="History",StID=3}, 28 new CourseStudent{CourseName="Physics",StID=3}, 29 }; 30 static void Main() 31 { 32 var query =from s in students//开始查询,在一个集合内查询 33 join c in studentsInCourses on s.StID equals c.StID 34 //而这个集合是,由两个集合的部分组成,上面的意思是: 35 //定义students的迭代变量a 36 //定义studentsInCourses迭代变量c 37 //联结这连个集合中的成员,并生成新的一个集合 38 //联结结果是:把两个集合成员具有相同StID的拿出来组成一个新的集合 39 where c.CourseName=="History" 40 //找出新集合中CourseName== "History"的成员 41 select s.LastName; 42 //把这些成员的LastName返回 43 foreach(var q in query) 44 Console.WriteLine("Student taking History: {0}", q); 45 Console.ReadKey(); 46 } 47 }
1 class Program 2 { 3 static void Main() 4 { 5 var groupA =new[]{3,4,5,6}; 6 var groupB =new[]{6,7,8,9}; 7 var someInts =from a in groupA//必须的第一个from语句 8 from b in groupB//主句中的from语句 9 select new{ a, b, sum=a+b };//创建一个匿名类型 10 foreach(var a in someInts ) 11 Console.WriteLine( a );//很神奇,"a=","b="自动写了 12 } 13 }
1 class Program 2 { 3 static void Main() 4 { 5 var groupA =new[]{3,4,5,6}; 6 var groupB =new[]{6,7,8,9}; 7 var someInts =from a in groupA 8 from b in groupB 9 let sum = a + b 10 where sum ==12 11 select new{ a, b, sum }; 12 foreach(var a in someInts ) 13 Console.WriteLine( a ); 14 } 15 }
1 static void Main() 2 { 3 var groupA =new[]{3,4,5,6}; 4 var groupB =new[]{6,7,8,9}; 5 var someInts =from int a in groupA 6 from int b in groupB 7 let sum = a + b 8 where sum >=11 9 where a ==4 10 select new{ a, b, sum }; 11 foreach(var a in someInts ) 12 Console.WriteLine( a ); 13 }
1 classProgram 2 { 3 static void Main() 4 { 5 var students =new[] 6 { 7 new{LName="Jones",FName="Mary",Age=19,Major="History"}, 8 new{LName="Smith",FName="Bob",Age=20,Major="CompSci"}, 9 new{LName="Fleming",FName="Carol",Age=21,Major="History"} 10 }; 11 var query =from student in students 12 orderby student.Age ascending//降序descending 13 select student; 14 foreach(var s in query) 15 { 16 Console.WriteLine("{0}, {1}: {2} - {3}", 17 s.LName, s.FName, s.Age, s.Major); 18 } 19 Console.ReadKey(); 20 } 21 }
1 classProgram 2 { 3 static void Main() 4 { 5 var students =new[] 6 { 7 new{LName="Jones",FName="Mary",Age=19,Major="History"}, 8 new{LName="Smith",FName="Bob",Age=20,Major="CompSci"}, 9 new{LName="Fleming",FName="Carol",Age=21,Major="History"} 10 }; 11 var query =from student in students 12 group student by student.Major; 13 //query是返回的是IEnumerable<IGrouping<键,值>>,所以下面不能直接打印 14 foreach(var s in query ) 15 { 16 Console.WriteLine("{0}", s.Key);//key是分组键 17 foreach(var t in s ) 18 Console.WriteLine(" {0}, {1}", t.LName, t.FName); 19 } 20 } 21 }
1 classProgram 2 { 3 static void Main() 4 { 5 var groupA =new[]{3,4,5,6}; 6 var groupB =new[]{4,5,6,7}; 7 var someInts =from a in groupA 8 join b in groupB on a equals b 9 into groupAandB 10 from c in groupAandB 11 //选中groupA与groupB相同的部分把这部分取名为grupAandB 12 select c; 13 foreach(var a in someInts ) 14 Console.Write("{0} ", a ); 15 } 16 }
标签:
原文地址:http://www.cnblogs.com/Recoding/p/4420984.html