zoukankan      html  css  js  c++  java
  • Linq

    一、LINQ查询表达式基础

      1、简单的 LINQ 查询

     1     static class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             List<Student> list = new List<Student>()
     6             {
     7                 new Student(){Id=1,Name="AA",score=60},
     8                 new Student(){Id=1,Name="BB",score=82},
     9                 new Student(){Id=1,Name="CC",score=90},
    10                 new Student(){Id=1,Name="DD",score=98},
    11                 new Student(){Id=1,Name="EE",score=76},
    12             };
    13             var itemList = from l in list
    14                            where l.score > 80
    15                            orderby l.score ascending
    16                            select l;
    17             foreach (var item in itemList)
    18             {
    19                 Console.WriteLine("{0}-{1}", item.Name, item.score);
    20             }
    21         }
    22     }
    23     /// <summary>
    24     /// Student Model
    25     /// </summary>
    26     public class Student
    27     {
    28         public int Id { set; get; }
    29 
    30         public string Name { set; get; }
    31 
    32         public int score { set; get; }
    33     }
    View Code

      2、Where 的使用

     1         static void Main(string[] args)
     2         {
     3             List<Student> list = new List<Student>()
     4             {
     5                 new Student(){Id=1,Name="AA",score=60},
     6                 new Student(){Id=1,Name="BB",score=82},
     7                 new Student(){Id=1,Name="CC",score=90},
     8                 new Student(){Id=1,Name="DD",score=84},
     9                 new Student(){Id=1,Name="EE",score=76},
    10             };
    11             var itemList = list.Where(p => p.score > 80);
    12             foreach (var item in itemList)
    13             {
    14                 Console.WriteLine("{0}-{1}", item.Name, item.score);
    15             }
    16         }
    View Code

      3、First,FirstOrDefault 和 Last,LastOrDefault;尽量使用 FirstOrDefault 和 LastOrDefault ,可避免 InvalidOperationException 异常

    1             var item1 = list.Where(p => p.score > 180).First();
    2             var item2 = list.Where(p => p.score > 180).FirstOrDefault();
    3             var item3 = list.Where(p => p.score > 180).Last();
    4             var item4 = list.Where(p => p.score > 180).LastOrDefault();
    View Code

    二、分组联接

     1  static void Main(string[] args)
     2         {
     3             List<Student> studentlist = new List<Student>()
     4             {
     5                 new Student(){Id=1,Name="AA",score=60},
     6                 new Student(){Id=2,Name="BB",score=82},
     7                 new Student(){Id=3,Name="CC",score=90},
     8                 new Student(){Id=4,Name="DD",score=84},
     9                 new Student(){Id=5,Name="EE",score=76},
    10             };
    11             List<Book> bookList = new List<Book>() 
    12             {
    13                 new Book(){Id=1,BokkName="C++"},
    14                 new Book(){Id=1,BokkName="C"},
    15                 new Book(){Id=2,BokkName="JAVA"},
    16                 new Book(){Id=2,BokkName="C#"},
    17                 new Book(){Id=3,BokkName="ASP.NET"},
    18             };
    19             var doubleModel = from s in studentlist
    20                               join b in bookList on s.Id equals b.Id
    21                               where s.Id == 1
    22                               select new { Name = s.Name, bookName = b.BokkName };
    23 
    24             foreach (var item in doubleModel)
    25             {
    26                 Console.WriteLine("{0}-{1}",item.Name,item.bookName);
    27             }
    28         }
    View Code

     三、自定义

      list.GroupBy(n => new { n.StudentName, n.StudentCode }).Select(x => x.First())

    未完待续。。。

  • 相关阅读:
    linux软件安装方式
    docker 安装 jenkins touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    [ERR] Node goodsleep.vip:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
    Linux 常用命令 服务器间scp 用户 export 创建文件、软连接
    redis 安装 集群 主从 哨兵 docker
    WPF密码框中禁止复制、粘贴
    Application 统计在线人数
    【转义字符】HTML 字符实体&lt; &gt: &amp;等
    SQL语句统计每天的数据
    正则表达式计算代码数
  • 原文地址:https://www.cnblogs.com/Jacob-Wu/p/5780710.html
Copyright © 2011-2022 走看看