• 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())

    未完待续。。。

  • 相关阅读:
    python面向对象
    Python基本数据类型
    小刘同学的第一百四十四篇博文
    小刘同学的第一百四十三篇日记
    小刘同学的第一百四十二篇日记
    小刘同学的第一百四十一篇日记
    小刘同学的第一百四十篇日记
    小刘同学的第一百三十九篇博文
    小刘同学的第一百三十八篇日记
    小刘同学的第一百三十七篇日记
  • 原文地址:https://www.cnblogs.com/Jacob-Wu/p/5780710.html
Copyright © 2020-2023  润新知