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

我们知道,一体化查询语言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#

四:数据库表查询

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

(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%的常见原因及优化方案

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/308699.html<

(0)
运维的头像运维
上一篇2025-05-27 17:11
下一篇 2025-05-27 17:12

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注