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.

48 lines
1.1 KiB
C#

6 years ago
using System;
6 years ago
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using Xunit;
namespace LinqStudy.Test.LinqToObject
{
/// <summary>
6 years ago
/// where操作符
6 years ago
/// </summary>
public class WhereTest
{
6 years ago
/// <summary>
/// where过滤查询条件
/// </summary>
6 years ago
[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);
}
6 years ago
/// <summary>
/// ArgumentNullException 异常
/// </summary>
6 years ago
[Fact]
6 years ago
public void Where_ArgumentNullException_Test()
6 years ago
{
List<Person> Persons = null;
Action act = ()=> { Persons.Where(p => p.Name.StartsWith("zhang")); };
Assert.Throws<ArgumentNullException>(act);
}
}
}