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.
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
#region Where
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// where:过滤查询条件
|
|
|
|
|
/// </summary>
|
|
|
|
|
[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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// ArgumentNullException 异常
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Where_ArgumentNullException_Test()
|
|
|
|
|
{
|
|
|
|
|
List<Person> Persons = null;
|
|
|
|
|
|
|
|
|
|
Action act = () => { Persons.Where(p => p.Name.StartsWith("zhang")); };
|
|
|
|
|
|
|
|
|
|
Assert.Throws<ArgumentNullException>(act);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|