方法All返回布爾值bool,判斷集合中是否所有元素都滿足某一條件,通俗一點說,就是每一個元素,均符合同一個條件,它才返回真,不然返回假。舉列,創建一個model: source code: namespace Insus.NET.Models { public class Book { publi ...
方法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
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