这一篇介绍了下面的内容:

  • 查询object数组
  • 查询强类型数组
  • 查询泛型字典
  • 查询字符串
  • SelectMany
  • 索引
  • Distinct操作符
  • 排序
  • 嵌套查询
  • 分组
  • 组连接
  • 内连接
  • 左外连接
  • 交叉连接
  • skip,take

详细请参看代码.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21.  
  22. }
  23.  
  24. private void button1_Click(object sender, EventArgs e)
  25. {
  26. //查询object数组
  27. Object[] array = { "String", , true, 'a' };
  28. var types = array
  29. .Select(s => s.GetType().Name)
  30. .OrderBy(s => s);
  31. ObjectDumper.Write(types);
  32. }
  33.  
  34. private void button2_Click(object sender, EventArgs e)
  35. {
  36. //查询强类型数组
  37. Book[] books ={
  38. new Book{Title="Linq in Action"},
  39. new Book{Title="Linq for Fun"},
  40. new Book{Title="Extreme LINQ"}
  41. };
  42.  
  43. var titles =
  44. books
  45. .Where(s => s.Title.Contains("Action"))
  46. .Select(s => s.Title);
  47.  
  48. ObjectDumper.Write(titles);
  49. }
  50.  
  51. private void button3_Click(object sender, EventArgs e)
  52. {
  53. //查询泛型字典
  54. var dic = new Dictionary<int, string>();
  55. dic.Add(, "zero");
  56. dic.Add(, "un");
  57. dic.Add(, "deux");
  58. dic.Add(, "trois");
  59. dic.Add(, "quatre");
  60. var list =
  61. dic
  62. .Where(s => s.Key % == )
  63. .Select(s => s.Value);
  64.  
  65. ObjectDumper.Write(list);
  66. }
  67.  
  68. private void button4_Click(object sender, EventArgs e)
  69. {
  70. //查询字符串, 字符串实现了IEnumerable<Char>接口
  71. var count = "Non-letter characters in this string: 6"
  72. .Where(s => !Char.IsLetter(s))
  73. .Count();
  74.  
  75. ObjectDumper.Write(count);
  76. }
  77.  
  78. private void button5_Click(object sender, EventArgs e)
  79. {
  80. string[] books ={"Funny Stories","All your base are belong to us",
  81. "LINQ rules","C# on Rails","Bonjour mon Amour"};
  82. var query =
  83. books
  84. .Where(s => s.Length > )
  85. .Select(s => new { Book = s.ToUpper() });
  86.  
  87. var query1 =
  88. from s in books
  89. where s.Length >
  90. orderby s
  91. select new { Book = s.ToUpper() };
  92.  
  93. dataGridView1.DataSource = query1.ToList();
  94. }
  95.  
  96. private void button6_Click(object sender, EventArgs e)
  97. {
  98. var query =
  99. from s in SampleData.Books
  100. where s.Title.Length >
  101. orderby s.Title
  102. select new { Book = s.Title, Price = s.Price };
  103.  
  104. dataGridView2.DataSource = query.ToList();
  105. }
  106.  
  107. private void button7_Click(object sender, EventArgs e)
  108. {
  109. //SelectMany
  110.  
  111. var query1 =
  112. SampleData.Books
  113. .SelectMany(s => s.Authors);
  114. ObjectDumper.Write(query1);
  115.  
  116. var query =
  117. from book in SampleData.Books
  118. from author in book.Authors
  119. select author.FirstName + author.LastName;
  120.  
  121. ObjectDumper.Write(query);
  122. }
  123.  
  124. private void button8_Click(object sender, EventArgs e)
  125. {
  126. var book =
  127. SampleData.Books
  128. .Select((s, no) => new { no, s.Title })
  129. .OrderBy(s => s.Title);
  130.  
  131. //无法翻译为等同的查询表达式语法
  132.  
  133. ObjectDumper.Write(book);
  134.  
  135. }
  136.  
  137. private void button9_Click(object sender, EventArgs e)
  138. {
  139. //Distinct操作符
  140. var query =
  141. SampleData.Books
  142. .SelectMany(s => s.Authors)
  143. .Select(s => s.FirstName + s.LastName);
  144. ObjectDumper.Write(query);
  145.  
  146. //去除重复的author,C#没有和distinct等同的查询表达式
  147. var query1 =
  148. SampleData.Books
  149. .SelectMany(s => s.Authors)
  150. .Distinct()
  151. .Select(s => s.FirstName + s.LastName);
  152. Console.WriteLine();
  153. ObjectDumper.Write(query1);
  154.  
  155. }
  156.  
  157. private void button10_Click(object sender, EventArgs e)
  158. {
  159. //排序
  160. var query =
  161. from s in SampleData.Books
  162. orderby s.Publisher.Name, s.Price descending, s.Title
  163. select new
  164. {
  165. Publisher=s.Publisher.Name,
  166. s.Price,
  167. s.Title
  168. };
  169. dataGridView1.DataSource = query.ToList();
  170. ObjectDumper.Write(query);
  171.  
  172. var query1 =
  173. SampleData.Books
  174. .OrderBy(s => s.Publisher.Name)
  175. .ThenByDescending(s => s.Price)
  176. .ThenBy(s => s.Title)
  177. .Select(s => new
  178. {
  179. publisher = s.Publisher.Name,
  180. s.Price,
  181. s.Title
  182. });
  183. Console.WriteLine();
  184. ObjectDumper.Write(query1);
  185.  
  186. }
  187.  
  188. private void button11_Click(object sender, EventArgs e)
  189. {
  190. //嵌套查询
  191.  
  192. var query =
  193. from s in SampleData.Publishers
  194. select new
  195. {
  196. item1 = s.Name,
  197. item2 =
  198. from book in SampleData.Books
  199. where book.Publisher.Name == s.Name
  200. select book
  201. };
  202.  
  203. foreach (var m in query)
  204. {
  205. Console.WriteLine(m.item1+":");
  206. foreach (var k in m.item2)
  207. {
  208. Console.WriteLine(k.Title);
  209. }
  210. Console.WriteLine();
  211. }
  212.  
  213. }
  214.  
  215. private void button12_Click(object sender, EventArgs e)
  216. {
  217. //分组
  218. //代码让图书按出版社分组.属于同一出版社的图书将被分到相同的组中.
  219. //在这个查询中,该分组publisherBooks对象是IGrouping<TKey,T>接口.
  220. //其中T为集合IEnumerable<Book>
  221.  
  222. var query =
  223. from s in SampleData.Books
  224. group s by s.Publisher into publisherBooks
  225. select new
  226. {
  227. publisher=publisherBooks.Key.Name,
  228. books=publisherBooks,
  229. booksum=publisherBooks.Count()
  230. };
  231. foreach (var m in query)
  232. {
  233. Console.WriteLine(m.publisher + ":");
  234. foreach (var k in m.books)
  235. {
  236. Console.WriteLine(k.Title);
  237. }
  238. Console.WriteLine(m.booksum);
  239. Console.WriteLine();
  240. }
  241.  
  242. }
  243.  
  244. private void button13_Click(object sender, EventArgs e)
  245. {
  246. //组连接
  247. var query =
  248. from s in SampleData.Publishers
  249. join book in SampleData.Books
  250. on s equals book.Publisher into publisherBooks
  251. select new
  252. {
  253. publisher=s.Name,
  254. books=publisherBooks
  255. };
  256.  
  257. foreach (var m in query)
  258. {
  259. Console.WriteLine(m.publisher + ":");
  260. foreach (var k in m.books)
  261. {
  262. Console.WriteLine(k.Title);
  263. }
  264. Console.WriteLine();
  265. }
  266.  
  267. }
  268.  
  269. private void button14_Click(object sender, EventArgs e)
  270. {
  271. //内连接,旨在找到两个序列的交集
  272. //和组连接的差别是,没有使用into关键字将元素分组,而是将图书投影在了出版社对象上
  273. //注意没有任何书籍的出版社没有显示出来,这是因为在两个待连接序列中均符合条件的组合才会出现在结果序列中.
  274. var query =
  275. from s in SampleData.Publishers
  276. join book in SampleData.Books on s equals book.Publisher
  277. select new
  278. {
  279. publisher=s.Name,
  280. books=book.Title
  281. };
  282.  
  283. var query2=
  284. SampleData.Publishers
  285. .Join(SampleData.Books, //内部序列
  286. s=>s, //外部的key选择器
  287. book=>book.Publisher, //内部的key选择器
  288. (s,book)=>new //结果选择器
  289. {
  290. publisher=s.Name,
  291. books=book.Title
  292. });
  293.  
  294. ObjectDumper.Write(query2);
  295.  
  296. foreach (var m in query)
  297. {
  298. Console.WriteLine(m.publisher + ":");
  299. Console.WriteLine(m.books);
  300. Console.WriteLine();
  301. }
  302.  
  303. }
  304.  
  305. private void button15_Click(object sender, EventArgs e)
  306. {
  307. //左外连接
  308. var query =
  309. from s in SampleData.Publishers
  310. join book in SampleData.Books
  311. on s equals book.Publisher into publisherBooks
  312. from book in publisherBooks.DefaultIfEmpty() //为空序列提供默认元素
  313. select new
  314. {
  315. publisher=s.Name,
  316. books=book==default(Book)?"(no books)":book.Title
  317. };
  318.  
  319. ObjectDumper.Write(query);
  320. }
  321.  
  322. private void button16_Click(object sender, EventArgs e)
  323. {
  324. //交叉连接, 计算出两个序列中所有元素的积
  325. //结果序列由一个序列的每个元素和另一个序列中每个元素的完全组合构成
  326. var query =
  327. from s in SampleData.Publishers
  328. from book in SampleData.Books
  329. select new
  330. {
  331. correct=(s==book.Publisher),
  332. publisher=s.Name,
  333. books=book.Title
  334. };
  335. ObjectDumper.Write(query);
  336. }
  337.  
  338. private void button17_Click(object sender, EventArgs e)
  339. {
  340. //skip,take
  341. var list1 = new List<string>();
  342. for (int i = ; i < ; i++)
  343. list1.Add(i.ToString());
  344.  
  345. var query =
  346. list1
  347. .Select(s=>s)
  348. .Skip().Take();
  349.  
  350. ObjectDumper.Write(query);
  351. }
  352.  
  353. }
  354.  
  355. }

本节源代码下载

原创文章,出自"博客园, 猪悟能'S博客" : http://www.cnblogs.com/hackpig/

LinQ实战学习笔记(四) LINQ to Object, 常用查询操作符的更多相关文章

  1. LinQ实战学习笔记(一) LINQ to (Objects, XML, SQL) 入门初步

    LINQ对于笔者来说, 优美而浓缩的代码让人震惊. 研究LINQ就是在艺术化自己的代码. 之前只是走马观花学会了基本的语法, 但是经常在CSDN看到令人惊讶自叹不如的LINQ代码, 还是让人羡慕嫉妒恨 ...

  2. LinQ实战学习笔记(三) 序列,查询操作符,查询表达式,表达式树

    序列 延迟查询执行 查询操作符 查询表达式 表达式树 (一) 序列 先上一段代码, 这段代码使用扩展方法实现下面的要求: 取进程列表,进行过滤(取大于10M的进程) 列表进行排序(按内存占用) 只保留 ...

  3. Entity Framework学习笔记(四)----Linq查询(1)

    请注明转载地址:http://www.cnblogs.com/arhat 从本章开始,老魏就介绍一下Entity Framework使用Linq来查询数据,也就是Linq To Entity.其实在E ...

  4. LinQ实战学习笔记(二) C#增强特性

    C# 为支持LINQ添加了许多语言特性: 隐式类型局部变量 对象初始化器 Lambda表达式 扩展方法 匿名类型 了解这些新特性是全面了解LINQ的重要先解条件,因此请不要忽视它们. (一)  隐式类 ...

  5. C#可扩展编程之MEF学习笔记(四):见证奇迹的时刻

    前面三篇讲了MEF的基础和基本到导入导出方法,下面就是见证MEF真正魅力所在的时刻.如果没有看过前面的文章,请到我的博客首页查看. 前面我们都是在一个项目中写了一个类来测试的,但实际开发中,我们往往要 ...

  6. java之jvm学习笔记四(安全管理器)

    java之jvm学习笔记四(安全管理器) 前面已经简述了java的安全模型的两个组成部分(类装载器,class文件校验器),接下来学习的是java安全模型的另外一个重要组成部分安全管理器. 安全管理器 ...

  7. ES6学习笔记<四> default、rest、Multi-line Strings

    default 参数默认值 在实际开发 有时需要给一些参数默认值. 在ES6之前一般都这么处理参数默认值 function add(val_1,val_2){ val_1 = val_1 || 10; ...

  8. python3.4学习笔记(四) 3.x和2.x的区别,持续更新

    python3.4学习笔记(四) 3.x和2.x的区别 在2.x中:print html,3.x中必须改成:print(html) import urllib2ImportError: No modu ...

  9. ZooKeeper学习笔记四:使用ZooKeeper实现一个简单的分布式锁

    作者:Grey 原文地址: ZooKeeper学习笔记四:使用ZooKeeper实现一个简单的分布式锁 前置知识 完成ZooKeeper集群搭建以及熟悉ZooKeeperAPI基本使用 需求 当多个进 ...

随机推荐

  1. Red Hat Enterprise Linux 各版本详细说明

    https://access.redhat.com/articles/3078#RHEL7 Red Hat Enterprise Linux Release Dates Updated Novembe ...

  2. STM32 SPI DMA 的使用

    一是想总结一下SPI总线的特点与注意点,二是总结一下SPI DMA的使用 一.SPI信号线说明 通常SPI通过4个引脚与外部器件相连: MISO:主设备输入/从设备输出引脚.该引脚在从模式下发送数据, ...

  3. [Tip] 如何在BeyondCompare中忽略不重要的区别.

    在使用BeyondCompare时,有时需要忽略一些不重要的区别,下面的链接教你如何通过定义语法元素来实现这个功能. http://www.scootersoftware.com/support.ph ...

  4. 基于nodejs实现js后端化处理

    今天D哥给我提了个问题,"用php执行过js没"?咋一听,没戏~~毕竟常规情况下,js是依赖浏览器运行的.想在php后端采集的同时利用js运行结果并传递给php使用,没戏! 然后回 ...

  5. 打包并压缩seajs代码

    背景 seajs是一款优秀的模块开发插件,但是当我们使用它来进行模块化开发的时候,由于它的每个模块的加载都会进行一次http请求,那么当模块数量倍增的时候,会拖慢页面的加载速度. 通常我们为了能加快页 ...

  6. 【cs229-Lecture19】微分动态规划

    内容: 调试强化学习算法(RL算法) LQR线性二次型调节(french动态规划算法) 滤波(kalman filters) 线性二次高斯控制(LGG) Kalman滤波器 卡尔曼滤波(Kalman ...

  7. 文件比对工具(Beyond Compare)

    文件比对工具: 工具名称:Beyond Compare 版本号:v3.3.13 下载地址:http://i.cnblogs.com/Files.aspx 官网最新版本下载地址:http://www.s ...

  8. SNF开发平台WinForm之九-代码生成器使用说明-SNF快速开发平台3.3-Spring.Net.Framework

    下面就具体的使用说明: 1.获取代码生成器的授权码(根据本机)-----还原数据库-------改config-----代码生成器 改代码生成器Config 2.登录代码生成器 3.查看是否连接成功 ...

  9. Python--matplotlib绘图可视化知识点整理

    from:https://segmentfault.com/a/1190000005104723 本文作为学习过程中对matplotlib一些常用知识点的整理,方便查找. 强烈推荐ipython无论你 ...

  10. NPM使用详解(下)

    NPM使用详解(下) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impo ...