1. Where
限制操作符Where用于过滤序列,按照提供的逻辑对序列中的数据进行过滤。
1>. 原型定义
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);
2>. 单个限制条件
var products = from p in context.Products where p.UnitPrice > 10m select p;
var products = context.Products .Where(p => p.UnitPrice > 10m);
3>. 多个过滤条件
var products = from p in context.Products where p.UnitPrice > 10m && p.ProductName.StartsWith("LINQ") select p;
var products = context.Products .Where(p => p.UnitPrice > 10m && p.ProductName.StartsWith("LINQ"));
4>.Lambda多参数表达式
int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 }; var expr = fibonacci.Where((f, index) => f > 1 && index > 3); foreach (var item in expr) { Console.WriteLine(item); }
时间: 2024-10-18 16:33:54