using System; using System.Collections.Generic; using System.Text; using System.Linq; using System.Linq.Expressions; using Xunit; namespace LinqStudy.Test.LinqToObject { public class TakeTest { [Fact] public void TakeWhile_Test() { List customers = new List { new Person { Id=1,Name="woody1"}, new Person { Id=2,Name="woody2"}, new Person { Id=3,Name="woody3"}, new Person { Id=4,Name="woody1"} }; var person = customers.TakeWhile(c => c.Name.StartsWith("woody1")); //因为匹配第二项时,结果是False,程序返回,不再进行下次迭代,所以返回集合中只有第一项。 Assert.Equal(customers.FindAll(q => q.Id == 1), person); } [Fact] public void Test() { List customers = new List { new Person { Id=1,Name="woody1"}, new Person { Id=2,Name="woody2"}, new Person { Id=3,Name="woody3"}, new Person { Id=4,Name="woody1"} }; var person = customers.TakeWhile(c => c.Name.StartsWith("woody2")); //因为匹配第一项时,结果是False,程序直接返回,不再进行下次迭代,所以返回空集合。 Assert.Empty(person); } } }