程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> LinQ實戰學習筆記(四) LINQ to Object, 常用查詢操作符,linq學習筆記

LinQ實戰學習筆記(四) LINQ to Object, 常用查詢操作符,linq學習筆記

編輯:關於.NET

LinQ實戰學習筆記(四) LINQ to Object, 常用查詢操作符,linq學習筆記


 

 這一篇介紹了下面的內容:

  • 查詢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", 12, 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(0, "zero");
 56             dic.Add(1, "un");
 57             dic.Add(2, "deux");
 58             dic.Add(3, "trois");
 59             dic.Add(4, "quatre");
 60             var list =
 61                 dic
 62                 .Where(s => s.Key % 2 == 0)
 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 > 10)
 85                     .Select(s => new { Book = s.ToUpper() });
 86 
 87             var query1 =
 88                 from s in books
 89                 where s.Length > 10
 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 > 5
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 
216         private void button12_Click(object sender, EventArgs e)
217         {
218             //分組
219             //代碼讓圖書按出版社分組.屬於同一出版社的圖書將被分到相同的組中.
220             //在這個查詢中,該分組publisherBooks對象是IGrouping<TKey,T>接口.
221             //其中T為集合IEnumerable<Book>
222 
223             var query =
224                 from s in SampleData.Books
225                 group s by s.Publisher into publisherBooks
226                 select new
227                 {
228                     publisher=publisherBooks.Key.Name,
229                     books=publisherBooks,
230                     booksum=publisherBooks.Count()
231                 };
232             foreach (var m in query)
233             {
234                 Console.WriteLine(m.publisher + ":");
235                 foreach (var k in m.books)
236                 {
237                     Console.WriteLine(k.Title);
238                 }
239                 Console.WriteLine(m.booksum);
240                 Console.WriteLine();
241             }
242              
243         }
244 
245         private void button13_Click(object sender, EventArgs e)
246         {
247             //組連接
248             var query =
249                 from s in SampleData.Publishers
250                 join book in SampleData.Books
251                 on s equals book.Publisher into publisherBooks
252                 select new
253                 {
254                     publisher=s.Name,
255                     books=publisherBooks
256                 };
257 
258             foreach (var m in query)
259             {
260                 Console.WriteLine(m.publisher + ":");
261                 foreach (var k in m.books)
262                 {
263                     Console.WriteLine(k.Title);
264                 }
265                 Console.WriteLine();
266             }
267 
268         }
269 
270         private void button14_Click(object sender, EventArgs e)
271         {
272             //內連接,旨在找到兩個序列的交集
273             //和組連接的差別是,沒有使用into關鍵字將元素分組,而是將圖書投影在了出版社對象上
274             //注意沒有任何書籍的出版社沒有顯示出來,這是因為在兩個待連接序列中均符合條件的組合才會出現在結果序列中.
275             var query =
276                 from s in SampleData.Publishers
277                 join book in SampleData.Books on s equals book.Publisher
278                 select new
279                 {
280                     publisher=s.Name,
281                     books=book.Title
282                 };
283 
284             var query2=
285                 SampleData.Publishers
286                     .Join(SampleData.Books,     //內部序列
287                         s=>s,                   //外部的key選擇器
288                         book=>book.Publisher,   //內部的key選擇器
289                         (s,book)=>new           //結果選擇器
290                         {
291                             publisher=s.Name,
292                             books=book.Title
293                         });
294 
295             ObjectDumper.Write(query2);
296 
297             foreach (var m in query)
298             {
299                 Console.WriteLine(m.publisher + ":");
300                 Console.WriteLine(m.books);
301                 Console.WriteLine();
302             }
303 
304         }
305 
306         private void button15_Click(object sender, EventArgs e)
307         {
308             //左外連接
309             var query =
310                 from s in SampleData.Publishers
311                 join book in SampleData.Books
312                     on s equals book.Publisher into publisherBooks
313                 from book in publisherBooks.DefaultIfEmpty()            //為空序列提供默認元素
314                 select new
315                 {
316                     publisher=s.Name,
317                     books=book==default(Book)?"(no books)":book.Title
318                 };
319 
320             ObjectDumper.Write(query);
321         }
322 
323         private void button16_Click(object sender, EventArgs e)
324         {
325             //交叉連接, 計算出兩個序列中所有元素的積
326             //結果序列由一個序列的每個元素和另一個序列中每個元素的完全組合構成
327             var query =
328                 from s in SampleData.Publishers
329                 from book in SampleData.Books
330                 select new
331                 {
332                     correct=(s==book.Publisher),
333                     publisher=s.Name,
334                     books=book.Title
335                 };
336             ObjectDumper.Write(query);   
337         }
338 
339         private void button17_Click(object sender, EventArgs e)
340         {
341             //skip,take
342             var list1 = new List<string>();
343             for (int i = 0; i < 100; i++)
344                 list1.Add(i.ToString());
345 
346             var query =
347                 list1
348                 .Select(s=>s)
349                 .Skip(90).Take(10);
350                 
351 
352             ObjectDumper.Write(query);
353         }
354 
355 
356 
357 
358 
359     }
360 
361   
362 }

 

本節源代碼下載

 

原創文章,出自"博客園, 豬悟能'S博客" : http://www.cnblogs.com/hackpig/

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved