• linq中first() firstordefault() last() lastOrDefault() single() singleOrDeafult


    一、firstordefault() 和 first()

        class Program
        {
            static void Main(string[] args)
            {
                List<Emp> list = new List<Emp> {
                 new Emp{ Id="001", Name="张三", Age="10", Address="河北石家庄"},
                 new Emp{ Id="002", Name="李四", Age="15", Address="山东济南"},
                 new Emp{ Id="003", Name="王五", Age="23", Address="河南郑州"}
                };
                //firstordefault 如果找不到返回默认值  first如果找不到会发生异常
                var emp = list.Where(m => m.Name == "张三").FirstOrDefault();
                Console.WriteLine(emp);
                var emp2 = list.Where(m => m.Name == "").First();
                Console.WriteLine(emp2);
            }
        }
        class Emp
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string Age { get; set; }
            public string Address { get; set; }
    
        }

    二、last() lastordefault()

      class Program
        {
            static void Main(string[] args)
            {
                List<Emp> list = new List<Emp> {
                 new Emp{ Id="001", Name="张三", Age="10", Address="河北石家庄"},
                 new Emp{ Id="002", Name="李四", Age="15", Address="山东济南"},
                 new Emp{ Id="003", Name="王五", Age="23", Address="河南郑州"},
                 new Emp{ Id="004", Name="王五", Age="45", Address="天津武清"}
                };
                //lastOrdefult 和last  如果找不到返回默认值  last如果找不到会发生异常
                var emp = list.Where(m => m.Name == "王五").LastOrDefault();
                Console.WriteLine(emp.Address);
                var emp2 = list.Where(m => m.Name == "").Last();
                Console.WriteLine(emp2);
            }
        }
        class Emp
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string Age { get; set; }
            public string Address { get; set; }
    
        }
    }

    三、singe() singleordefault 唯一结果查询,超过一个结果报异常

         static void Main(string[] args)
            {
                List<Emp> list = new List<Emp> {
                 new Emp{ Id="001", Name="张三", Age="10", Address="河北石家庄"},
                 new Emp{ Id="002", Name="李四", Age="15", Address="山东济南"},
                 new Emp{ Id="003", Name="王五", Age="23", Address="河南郑州"},
                 new Emp{ Id="004", Name="王五", Age="45", Address="天津武清"}
                };
               // single 和singleordefault 如果序列超过一个会报异常,常用唯一结果查询
                var emp = list.Where(m => m.Name == "王五").SingleOrDefault();
                Console.WriteLine(emp.Address);
                var emp2 = list.Where(m => m.Name == "王五").Single();
                Console.WriteLine(emp2);
            }
        }
        class Emp
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string Age { get; set; }
            public string Address { get; set; }
    
        }

  • 相关阅读:
    Luogu1053 NOIP2005篝火晚会
    BZOJ2151 种树(贪心+堆+链表/wqs二分+动态规划)
    Luogu1155 NOIP2008双栈排序(并查集)
    Luogu1092 NOIP2004虫食算(搜索+高斯消元)
    Codeforces Round#516 Div.1 翻车记
    Luogu1731 NOI1999生日蛋糕(搜索)
    洛谷 P1379 八数码难题 解题报告
    洛谷 P2501 [HAOI2006]数字序列 解题报告
    洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告
    洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告
  • 原文地址:https://www.cnblogs.com/ItDotNet/p/4980821.html
Copyright © 2020-2023  润新知