码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-11-20 15:19:22      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

一、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; }

    }

技术分享

 

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

标签:

原文地址:http://www.cnblogs.com/ItDotNet/p/4980821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!