码迷,mamicode.com
首页 > 其他好文 > 详细

LINQ系列:限制操作符

时间:2014-10-21 17:40:44      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   strong   sp   数据   div   

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);
}

 

LINQ系列:限制操作符

标签:style   blog   color   ar   for   strong   sp   数据   div   

原文地址:http://www.cnblogs.com/libingql/p/4040806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!