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.

44 lines
1.3 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Linq To Object
> Linq To Object是操作内存对象。
>
> 使用前提对象必须实现IEnumerable或`IEnumerable<T>`接口。
>
> 因为IQueryable和`IQueryable<T>`也实现了IEnumerable或`IEnumerable<T>接口`所以也可以使用Linq To Object。
> 泛型集合均实现了`IEnumerable<T>`接口,可以直接使用。
> 一些集合类,也提供了转换扩展方法,可以转换后使用。
>
## Linq To Object本质
> 本质是扩展方法,没有相应的 Linq Provider这点与 Linq To SQL不同。
## 注意事项
+ 延时执行
> 写好操作步骤时,只是进行了“定义”,直到调用(转换方法、聚合方法会立即执行)时才会真正执行操作。这点特别注意。
+ 异常
> 当对象为null时通常都会抛出“参数null值”异常。使用前就确保对象不为null
``` csharp
[Fact]
public void Null_Test()
{
List<Person> person = null;
//对Linq操作符而言基本上数据源为Null时将引发异常。
Assert.ThrowsAny<ArgumentNullException>(() =>
{
//此处引发异常
var query = person.Where(p => p == null);
});
}
```
+ 操作结果
> 当操作结果没有数据项时不是返回null,而是返回“0数据项”的IEnumerable对象。对结果**不用进行null值判断**