using System; using System.Collections.Generic; using System.Text; using System.Linq; using System.Linq.Expressions; using FluentAssertions; using Xunit; namespace LinqStudy.Test.LinqToObject { /// /// 基本项测试 /// 异常、空值等项 /// public class LinqTest { [Fact] public void Null_Test() { List person = null; //对Linq操作符而言,基本上数据源为Null时,将引发异常。 Assert.ThrowsAny(() => { var query = person.Where(p => p == null); }); } [Fact] public void Count_0_Test() { List person = new List(); //数据源为没有任何内容项时,即 Count=0,不会引发异常。 Action action = () => { //查不到任何数据,不返回null,而是返回 Count=0的对象。 person.Where(p => p == null).ToList(); }; action.Should().NotThrow(); } } }