|
|
@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace LinqStudy.Test.LinqToObject
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// where操作符:过滤查询条件
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
public class WhereTest
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Where_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var Persons = new List<Person>()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
new Person(){ Id=1,Name="zhangsan",Age=2},
|
|
|
|
|
|
|
|
new Person(){ Id =2,Name="lishi",Age=33}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var query = Persons.Where(p => p.Name.StartsWith("zhang"));
|
|
|
|
|
|
|
|
var age = query.FirstOrDefault()?.Age;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal(2, age);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Where_Argm_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
List<Person> Persons = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Action act = ()=> { Persons.Where(p => p.Name.StartsWith("zhang")); };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Throws<ArgumentNullException>(act);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|