Linq 入门

简单来说sql查数据库,而Linq查对象。这个对象必须是

Linq主要包括4个组件:Linq to Objects,Linq to SQL,Linq to DataSet,Linq to XML

思考Linq查询的3个步骤:1、确定数据库源  2、编写查询表达式  3、执行查询方法

Linq最基本的4个接口:所有支持Linq查询的对象必须直接或间接实现IEnumerable接口
  IEnumerable
  IEnumerable<T>
  IQueryable
  IQueryable<T>

Enumerable类扩展了并实现了IEnumberable<T>接口的对象的静态方法,50个方法

序列号静态方法说明
1 Aggregate() 对集合应用累加函数
2 All<T>() 判断集合中的所有元素是否满足条件
3 Any() 判断集合中是否存在满足条件
4 AsEnumerable<T>() 将数据源转换为IEnumerable<T>类型的对象

 

IQueryable 和IQueryable<T>接口:提供对未指定数据类型的数据源进行计算的功能,它一般由查询提供程序实现。
IQueryable接口提供的AsQueryable()方法可以将IEnumerable类型的对象转换为IQueryable类型的对象


Linq

Lambda

SQL

说明

from c in Customers
select c
Customers
   .Select (c => c)
select * from Customers
查询一个表的所有数据
from c in Customers
select c.ContactName
Customers
   .Select (c => c.ContactName)
select ContactName from Customers
投影查询(一个列)
from c in Customers
select new
{
    公司名称 = c.CompanyName,
    地址 = c.Address
}
Customers
   .Select (
      c => 
         new  
         {
            公司名称 = c.CompanyName, 
            地址 = c.Address
         }
   )
select CompanyName as 公司名称, Address as 地址 from Customers

投影查询(多列),其中Lambda

使用了new关键字,表示

创建匿名类型

from c in Customers
where c.CompanyName == "Alfreds Futterkiste"
select c
Customers
   .Where (c => (c.CompanyName == "Alfreds Futterkiste"))
select * from Customers 
where CompanyName = 'Alfreds Futterkiste'
条件查询
from p in Products
group p by p.CategoryID
 
Products
   .GroupBy (p => p.CategoryID)
 
select * from Products where CategoryID = 1;
select * from Products where CategoryID = 2;
select * from Products where CategoryID = 3;
...
/* 分类统计
select count(*) from Products 
group by CategoryID
*/

 因为linq 和sql在分组上有些概念不一致,

导致表达方式上的不一致。linq分组后会

以分组的key为分类边界,在边界内进行

过滤查询,形成一个子集,后面的子集类似。

而sql是表格式的结果,只能和一个统计字段

配合,然后以分组字段合计成一个结果集。

       
       
       
       

分组示例1

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace linqDemo
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             var source = GetData();
14 
15             var query = (from s in source
16                 group s by s.Name
17                 into grouped
18                 orderby grouped.Sum(m => m.Score)
19                 select new
20                 {
21                     Name = grouped.Key,
22                     Scores = grouped.Sum(m => m.Score)
23                 }).ToList();
24 
25             foreach (var s in query)
26             {
27                 Console.WriteLine("{0}:总分{1}", s.Name, s.Scores);
28             }
29 
30             Console.ReadKey();
31 
32         }
33 
34         public static List<StudentScore> GetData()
35         {
36             return new List<StudentScore>()
37             {
38                 new StudentScore() {ID = 1, Name = "张三", Term = "第一学期", Course = "Math", Score = 80},
39                 new StudentScore() {ID = 1, Name = "张三", Term = "第一学期", Course = "Chinese", Score = 90},
40                 new StudentScore() {ID = 1, Name = "张三", Term = "第一学期", Course = "English", Score = 70},
41                 new StudentScore() {ID = 2, Name = "李四", Term = "第一学期", Course = "Math", Score = 60},
42                 new StudentScore() {ID = 2, Name = "李四", Term = "第一学期", Course = "Chinese", Score = 70},
43                 new StudentScore() {ID = 2, Name = "李四", Term = "第一学期", Course = "English", Score = 30},
44                 new StudentScore() {ID = 3, Name = "王五", Term = "第一学期", Course = "Math", Score = 100},
45                 new StudentScore() {ID = 3, Name = "王五", Term = "第一学期", Course = "Chinese", Score = 80},
46                 new StudentScore() {ID = 3, Name = "王五", Term = "第一学期", Course = "English", Score = 80},
47                 new StudentScore() {ID = 4, Name = "赵六", Term = "第一学期", Course = "Math", Score = 90},
48                 new StudentScore() {ID = 4, Name = "赵六", Term = "第一学期", Course = "Chinese", Score = 80},
49                 new StudentScore() {ID = 4, Name = "赵六", Term = "第一学期", Course = "English", Score = 70},
50                 new StudentScore() {ID = 1, Name = "张三", Term = "第二学期", Course = "Math", Score = 100},
51                 new StudentScore() {ID = 1, Name = "张三", Term = "第二学期", Course = "Chinese", Score = 80},
52                 new StudentScore() {ID = 1, Name = "张三", Term = "第二学期", Course = "English", Score = 70},
53                 new StudentScore() {ID = 2, Name = "李四", Term = "第二学期", Course = "Math", Score = 90},
54                 new StudentScore() {ID = 2, Name = "李四", Term = "第二学期", Course = "Chinese", Score = 50},
55                 new StudentScore() {ID = 2, Name = "李四", Term = "第二学期", Course = "English", Score = 80},
56                 new StudentScore() {ID = 3, Name = "王五", Term = "第二学期", Course = "Math", Score = 90},
57                 new StudentScore() {ID = 3, Name = "王五", Term = "第二学期", Course = "Chinese", Score = 70},
58                 new StudentScore() {ID = 3, Name = "王五", Term = "第二学期", Course = "English", Score = 80},
59                 new StudentScore() {ID = 4, Name = "赵六", Term = "第二学期", Course = "Math", Score = 70},
60                 new StudentScore() {ID = 4, Name = "赵六", Term = "第二学期", Course = "Chinese", Score = 60},
61                 new StudentScore() {ID = 4, Name = "赵六", Term = "第二学期", Course = "English", Score = 70},
62             };
63         }
64     }
65 
66     public class StudentScore
67     {
68         public int ID { set; get; }
69         public string Name { set; get; }
70         public string Course { set; get; }
71         public int Score { set; get; }
72         public string Term { set; get; }
73     }
74 }
View Code

运行结果:

Lambda写法 

//Lambda
            var query2 = source.GroupBy(m => m.Name)
                .Select(k => new {Name = k.Key, Scores = k.Sum(l => l.Score)})
                .OrderBy(m => m.Scores).ToList();

分组示例2

当分组的字段是多个的时候,通常把这多个字段合并成一个匿名对象,然后group by这个匿名对象。

  

posted @ 2016-04-06 14:13  海阔天空XM  阅读(246)  评论(0)    收藏  举报