一体化查询语言LINQ的操作详解

数据库 SQL Server
本文主要介绍了一体化查询语言LINQ的一些操作,包括对字符串的查询、数组查询、XML查询和数据库表的查询等,希望能够对您有所帮助。

我们知道,一体化查询语言LINQ,即(NET Language Integrated Query),是集成在 .NET Framework 3.5 编程语言中的一种新特性,已成为编程语言的一部分,使开发人员可以使用语法基本一致的语句对不同来源不同类型的数据进行查询与整合,它使得查询表达式可以得到很好的编译时语法检查。

一:字符串查询

  1. string strLinq = "Hello World!";  
  2.  
  3. var result = from q in strLinq select q;  
  4.  
  5. var result1 = from q in strLinq where q == 'o' select q;  
  6.  
  7. // Linq 的扩展方法  
  8.  
  9. var result2 = strLinq.Where(q => q == 'o');  
  10.  
  11. var result3 = Enumerable.Where(strLinq, q => q == 'o'); 

二:数组查询

  1. string[] strs ={ "Suyama", "Fuller", "Callahan", "Michael", "Janet" };  
  2.  
  3. var result = from p in strs where p.Length > 5 select p;  
  4.  
  5. var result1 = strs.Where(p => p.Length>5);  
  6.  
  7. var result2 = strs.Where(p =>p.StartsWith("C")); 

三:XML 查询

  1. <Employees> 
  2.  
  3. <Employee gender="0"> 
  4.  
  5. <Name>Davolio</Name> 
  6.  
  7. <Address>507 - 20th Ave. E. Apt. 2A</Address> 
  8.  
  9. </Employee> 
  10.  
  11. <Employee gender="0"> 
  12.  
  13. <Name>Andrew</Name> 
  14.  
  15. <Address>4110 Old Redmond Rd.</Address> 
  16.  
  17. </Employee> 
  18.  
  19. <Employee gender="1"> 
  20.  
  21. <Name>Laura</Name> 
  22.  
  23. <Address>Coventry House Miner Rd.</Address> 
  24.  
  25. </Employee> 
  26.  
  27. <Employee gender="1"> 
  28.  
  29. <Name>Anne</Name> 
  30.  
  31. <Address>4726 - 11th Ave. N.E.</Address> 
  32.  
  33. </Employee> 
  34.  
  35. <Employee gender="0"> 
  36.  
  37. <Name>King</Name> 
  38.  
  39. <Address>7 Houndstooth Rd.</Address> 
  40.  
  41. </Employee> 
  42.  
  43. </Employees> 
  44.  
  45. XElement root = XElement.Load("D:\\Employees.xml");  
  46.  
  47. // 查询性别为男性(gender=1)的全部员工  
  48.  
  49. var emps = from s in root.Elements("Employee")  
  50.  
  51. where s.Attribute("gender").Value == "1"  
  52.  
  53. select s;  
  54.  
  55. // 查询性别为女性(gender=0)的员工的姓名和住址  
  56.  
  57. var emps1 = from s in root.Elements("Employee")  
  58.  
  59. where s.Attribute("gender").Value == "0"  
  60.  
  61. select new  
  62.  
  63. {  
  64.  
  65. name = s.Element("Name").Value,  
  66.  
  67. address = s.Element("Address").Value  
  68.  
  69. }; 

#p#

四:数据库表查询

其中用到的表的结构以及数据大致为下图所示:

一体化查询语言LINO的操作详解

一体化查询语言LINO的操作详解

(1) 单表查询

  1. // <1> 查询所有客户  
  2.  
  3. var customers1 = from s in dc.Customers select s;  
  4.  
  5. List<Customer> customerLst1 = customers1.ToList();  
  6.  
  7. // <2> 查询国籍为 'Germany' 的客户  
  8.  
  9. var customers2 = from s in dc.Customers where s.Country == "Germany" select s;  
  10.  
  11. List<Customer> customerLst2 = customers2.ToList();  
  12.  
  13. List<Customer> customerLst2_1 = (from s in dc.Customers where s.Country == "Germany" select s).ToList();  
  14.  
  15. List<Customer> customerLst2_2 = dc.Customers.Where(s => s.Country == "Germany").ToList();  
  16.  
  17. // <3> 按公司名降序排列查询员工ID和公司  
  18.  
  19. var customerLst3 = (from s in dc.Customers  
  20.  
  21. orderby s.CompanyName descending  
  22.  
  23. select new  
  24.  
  25. {  
  26.  
  27. ID = s.CustomerID,  
  28.  
  29. Company = s.CompanyName  
  30.  
  31. }).ToList();  
  32.  
  33. // <4> 查询公司名,并判断其长度是不是大于20  
  34.  
  35. var customerLst4 = (from s in dc.Customers  
  36.  
  37. select new  
  38.  
  39. {  
  40.  
  41. CompanyName = s.CompanyName,  
  42.  
  43. IsThan20 = (s.CompanyName.Length > 20) ? true : false  
  44.  
  45. }).ToList();  
  46.  
  47. // <5> 按顺序查询第10到20记录的客户  
  48.  
  49. List<Customer> customerLst5 = (from s in dc.Customers select s).Skip(9).Take(11).ToList();  
  50.  
  51. // Skip(9): 跳过前9个  
  52.  
  53. // Take(11): 取前11条记录  
  54.  
  55. // <6> 国籍为 Canada和USA的客户  
  56.  
  57. var customerLst6 = from s in dc.Customers where new string[] { "Canada", "USA" }.Contains(s.Country) select s;  
  58.  
  59. // <7> 地址中有 '9'  
  60.  
  61. var customerLst7 = from s in dc.Customers where s.Address.Contains("9") select s;  
  62.  
  63. // <8> 地址以 'A' 开头  
  64.  
  65. var customerLst8 = from s in dc.Customers where s.Address.StartsWith("A") select s;  
  66.  
  67. var customerLst8_1 = from s in dc.Customers where s.Address.IndexOf("A") == 0 select s;  
  68.  
  69. // <9> 地址以 'A' 开头的客户数量  
  70.  
  71. var customerLst9 = dc.Customers.Count(s => s.Address.IndexOf("A") == 0);  
  72.  
  73. // <10> 查询最高、最低、平均、总共的付款  
  74.  
  75. var customerLst10 = dc.Customers.Select(s => s.Payment).Max();  
  76.  
  77. var customerLst10_1 = Enumerable.Select(dc.Customers, s => s.Payment).Max();  
  78.  
  79. var customerLst10_2 = dc.Customers.Select(s => s.Payment).Min();  
  80.  
  81. var customerLst10_3 = dc.Customers.Select(s => s.Payment).Average();  
  82.  
  83. var customerLst10_4 = dc.Customers.Select(s => s.Payment).Sum();  
  84.  
  85. // <11> 按国籍查询客户数量和最高付款  
  86.  
  87. var customerLst11 = (from s in dc.Customers  
  88.  
  89. group s by s.Country into p  
  90.  
  91. select new  
  92.  
  93. {  
  94.  
  95. Country = p.Key,  
  96.  
  97. Count = p.Count(),  
  98.  
  99. Payment = p.Max(g => g.Payment)  
  100.  
  101. }).ToList(); 

#p#

(2) 多表查询

  1. // <1> 查询每个客户下订单的日期 (内连接)  
  2.  
  3. var customerLst1 = from s in dc.Customers  
  4.  
  5. join p in dc.Orders on s.CustomerID equals p.CustomerID  
  6.  
  7. orderby s.CustomerID ascending  
  8.  
  9. select new  
  10.  
  11. {  
  12.  
  13. ID = s.CustomerID,  
  14.  
  15. OrderDate = p.OrderDate  
  16.  
  17. };  
  18.  
  19. // <2> 查询每个客户下订单的日期 (左外连接)  
  20.  
  21. var customerLst2 = from s in dc.Customers  
  22.  
  23. join p in dc.Orders on s.CustomerID equals p.CustomerID  
  24.  
  25. into sp from a in sp.DefaultIfEmpty()  
  26.  
  27. orderby s.CustomerID ascending  
  28.  
  29. select new  
  30.  
  31. {  
  32.  
  33. ID = s.CustomerID,  
  34.  
  35. OrderDate = a.OrderDate  
  36.  
  37. };  
  38.  
  39. // <3> 查询每个客户下订单的日期,条件:付款大于200且订单日期在1997-12-08以后 (多表条件查询)  
  40.  
  41. var customerLst3 = from s in dc.Customers  
  42.  
  43. join p in dc.Orders on s.CustomerID equals p.CustomerID  
  44.  
  45. where s.Payment > 200 && p.OrderDate > DateTime.Parse("1997-12-08")  
  46.  
  47. orderby s.CustomerID ascending  
  48.  
  49. select new  
  50.  
  51. {  
  52.  
  53. ID = s.CustomerID,  
  54.  
  55. OrderDate = p.OrderDate  
  56.  
  57. }; 

实际操作起来,用linq进行多表连接比较复杂(特别是在表很多的情况下),建议先将查询内容做成视图,再把视图映射成实体类,这样就可以用单表查询的方式进行查询了。

作者建议:LINQ在做对数据库查询的时候,其实就是对sql的再封装,从而使得操作更加简洁,而且从LINQ解析得到的sql也得到了优化,可能针对某些查询,比没有优化的sql查询效率更高些,所以,如果开发人员追求效率(对数据的增、删、改、查),建议先做存储过程,然后优化,再把这些优化的存储过程在DataContext里面封装成方法,再调用这些方法,这样既可以把sql写的更好,效率又更高,而且还对提高个人的数据库知识水平也有很大帮助。

关于一体化查询语言LINQ的操作就介绍到这里了,希望通过本次的介绍能够带给您一些收获,谢谢各位浏览!

【编辑推荐】

  1. SQL Server数据库主键及复合主键的配置
  2. SQL SERVER 数据挖掘之理解内容类型
  3. 使用SSMA将Oracle数据库转成SQL Server 2008
  4. SQL Server数据库如何更改SA密码和默认端口号
  5. SQL Server引起CPU使用率是100%的常见原因及优化方案
责任编辑:赵鹏 来源: 博客园
相关推荐

2009-09-07 23:09:17

2009-05-13 08:21:11

SUSELinux桌面

2009-05-13 08:22:53

SUSELinux桌面

2017-05-16 10:46:06

博阳咨询流程管理

2023-07-19 22:13:25

一体化推送平台

2009-07-02 09:32:00

2009-12-03 15:34:41

Suse Linux

2011-05-24 09:26:02

有线无线3G

2009-08-17 22:32:25

IT运维管理监控运维一体化摩卡

2009-09-01 22:45:46

2013-10-15 11:12:50

2014-08-18 13:28:54

IT运维

2017-10-18 22:46:57

数据中心网络通信技术

2012-05-07 17:09:52

2023-09-17 17:59:28

边缘计算调度方案

2023-11-16 13:24:39

OceanBase数据库

2014-12-25 11:25:31

2014-10-14 10:45:18

用友

2009-03-19 09:50:00

华为机房一体化
点赞
收藏

51CTO技术栈公众号