You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.4 KiB
C#

6 years ago
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<Person> customers = new List<Person> {
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<Person> customers = new List<Person> {
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);
}
}
}