首页 > 其他 > 详细

LINQ的All的方法

时间:2016-04-13 14:42:41      阅读:435      评论:0      收藏:0      [点我收藏+]

方法All返回布尔值bool,判断集合中是否所有元素都满足某一条件,通俗一点说,就是每一个元素,均符合同一个条件,它才返回真,不然返回假。

举列,创建一个model:
技术分享

 

source code:

技术分享
namespace Insus.NET.Models
{
   public class Book
    {
        public string Publishing { get; set; }

        public string ISBN { get; set; }

        public DateTime PublicationDate  { get; set; }
    }
}
View Code


在ASP.NET MVC的视图片中,实现:
技术分享

source code:

技术分享
 Book[] books = { new Book { Publishing = "商务出版社",ISBN="13468564394",PublicationDate=Convert.ToDateTime("2016-04-13")},
                             new Book { Publishing = "中华出版社",ISBN="56634565746",PublicationDate=Convert.ToDateTime("2016-01-19")},
                             new Book { Publishing = "海天出版社",ISBN="78234235454",PublicationDate=Convert.ToDateTime("2016-03-22")},
                             new Book { Publishing = "云贵出版社",ISBN="46724356756",PublicationDate=Convert.ToDateTime("2016-02-22")}
            };


            IEnumerable<Book> query = from bk in books
                                      where books.All(b => b.PublicationDate > Convert.ToDateTime("2016-01-01"))
                                      select bk;


            query.ForEach(delegate (Book b)
            {
                WriteLiteral(string.Format("publishing: {0}; ISBN: {1}; Publication Date: {2} <br />",
                                            b.Publishing,
                                            b.ISBN,
                                            b.PublicationDate.ToString("yyyy-MM-dd")));
            });
View Code


更好理解,修改一下:
技术分享

 

LINQ的All的方法

原文:http://www.cnblogs.com/insus/p/5385754.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!