• LinQ实战学习笔记(四) LINQ to Object, 常用查询操作符


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

    • 查询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/

  • 相关阅读:
    iOS Graphics 编程
    如何用PHP/MySQL为 iOS App 写一个简单的web服务器(译) PART1
    Python服务器开发二:Python网络基础
    Access一些问题
    托管调试助手报错
    ConnectionString
    百度也开源
    Microsoft SQL Server 错误代号: 15535 解决方法
    临时表的一个用法
    类型初始值设定项引发异常
  • 原文地址:https://www.cnblogs.com/hackpig/p/5983350.html
Copyright © 2020-2023  润新知