LINQ的All的方法

方法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


更好理解,修改一下:


 

posted @ 2016-04-13 14:20  Insus.NET  阅读(2522)  评论(0编辑  收藏  举报