1. Where

  限制操作符Where用于过滤序列,按照提供的逻辑对序列中的数据进行过滤。Where可以出现多次。

1.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);

1.2 单个限制条件

var products = from p in context.Products
where p.UnitPrice > 10m
select p;
var products = context.Products
.Where(p => p.UnitPrice > 10m);
Func<Product, bool> filter = delegate(Product p) { return p.UnitPrice > 10m; };
var query = context.Products
.Where(filter)
.Select(p => new { p.ProductID, p.ProductName });
// using System.Linq;
int[] fibonacci = new int[] { , , , , , , , };
IEnumerable<int> query = Enumerable.Where(fibonacci, f => f > );
query.ToList().ForEach(f =>
{
Console.WriteLine(f);
});

1.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"));
var expr = context.Products
.Where(p => p.UnitPrice > 10m)
.Where(p => p.ProductName.StartsWith("LINQ"));

1.4 Lambda多参数表达式

int[] fibonacci = new int[] { , , , , , , ,  };
var expr = fibonacci.Where((f, index) => f > && index > );
foreach (var item in expr)
{
Console.WriteLine(item);
}

1.5 自定义实现

  LINQ是在C#3.0出现的,在C#2.0及之前没有LINQ的支持,接下来为LINQ Where操作符的自定义实现。

  C#2.0实现方式:

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
using System;
using System.Collections.Generic;
using System.Text; namespace Northwind.CustomLINQExtension
{
public static class CustomLINQExtension
{
public delegate TResult Func<in T, out TResult>(T arg);
public static IEnumerable<TSource> Where<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
foreach (TSource element in source)
{
if (predicate(element))
{
yield return element;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text; namespace Northwind.CustomLINQExtension
{
class Program
{
static void Main(string[] args)
{
int[] fibonacci = new int[] { , , , , , , , }; IEnumerable<int> expr = CustomLINQExtension.Where(fibonacci, (delegate(int i) { return i > ; }));
foreach (int item in expr)
{
Console.WriteLine(item);
}
}
}
}

  由于在C#2.0中没有扩展方法,调用实现的自定义扩展类需要使用类名。

  在C#3.0中增加了扩展方法,在C#3.0自定义LINQ Where限制条件,不使用系统LINQ自带。

using System;
using System.Collections.Generic;
using System.Text; namespace Northwind.CustomLINQExtension
{
public static class CustomLINQExtension
{
public delegate TResult Func<in T, out TResult>(T arg);
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
foreach (TSource element in source)
{
if (predicate(element))
{
yield return element;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text; namespace Northwind.CustomLINQExtension
{
class Program
{
static void Main(string[] args)
{
int[] fibonacci = new int[] { , , , , , , , }; IEnumerable<int> expr = fibonacci.Where(delegate(int i) { return i > ; });
foreach (int item in expr)
{
Console.WriteLine(item);
}
}
}
}

LINQ系列:Linq to Object限制操作符的更多相关文章

  1. C# ~ 从 XML 到 Linq 到 Linq to XML

    .XML 可扩展标记语言 (Extensible Markup Language), 标记 (markup) 是关键部分,是标准通用标记语言 (Standard Generalized Markup ...

  2. LINQ系列:Linq to Object集合操作符

    集合操作符对元素的集合或序列集合进行操作,并返回一个集合.LINQ共有4种集合查询操作符:Distinct.Union.Intersect和Except. 1. Distinct Distinct操作 ...

  3. LINQ系列目录

    1. LINQ准备 1.1 C#中与LINQ相关特性 2. LINQ to Object 2.1 LINQ to Object投影操作符(Select/SelectMany/Let) 2.2 LINQ ...

  4. LinQ系列文章

    温故而知新,想着系统再学习一次LinQ知识点,发现园子里有个非常棒的系列文章,所以Mark下来,方便以后查阅! 系列博客导航: LINQ之路系列博客导航 LINQ之路 1:LINQ介绍 LINQ之路 ...

  5. C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换

    LINQ to DataSet需要使用System.Core.dll.System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System. ...

  6. LINQ 系列

    C#图解教程 第十九章 LINQ   LINQ 什么是LINQLINQ提供程序 匿名类型 方法语法和查询语法查询变量查询表达式的结构 from子句join子句什么是联结查询主体中的from…let…w ...

  7. Linq系列

    LINQ 图解 Linq中的Select——投影 Linq学习资源 Expert C# 5.0中的Linq部分

  8. LINQ之LINQ to Objects(上)

    LINQ概述 LINQ,语言集成查询(Language Integrated Query),它允许使用C#或VB代码以查询数据库相同的方式来操作不同的数据源. 1.LINQ体系结构 从上图可以看出,L ...

  9. Linq之Linq to Sql

    目录 写在前面 系列文章 Linq to sql 总结 写在前面 上篇文章介绍了linq to xml的相关内容,linq to xml提供一种更便捷的创建xml树,及查询的途径.这篇文章将继续介绍l ...

  10. Linq之Linq to XML

    目录 写在前面 系列文章 linq to xml 总结 写在前面 在很多情况下,都可以见到使用xml的影子.例如,在 Web 上,在配置文件.Microsoft Office Word 文件(将wor ...

随机推荐

  1. 常用CSS缩写语法总结

    使用缩写可以帮助减少你CSS文件的大小,更加容易阅读.css缩写的主要规则如下: 颜色 16进制的色彩值,如果每两位的值相同,可以缩写一半,例如:#000000可以缩写为#000;#336699可以缩 ...

  2. Node.js在Chrome进行调试

    在开发node.js环境时候,调试是一件很疼苦的事情,不过随着时代不断发展,先如今已经有很多种node环境代码调试方式,今天我就笔记一下我使用的方式 node-inspector: node-insp ...

  3. SSDB 主从配置

    环境 Master/Slave     系统 IP SSDB版本 Master     CentOS6.7         10.10.3.211         ssdb-1.8.0     Sla ...

  4. clientX .offsetX .screenX x 的区别

    clientX 设置或获取鼠标指针位置相对于当前窗口的 x 坐标,其中客户区域不包括窗口自身的控件和滚动条. clientY 设置或获取鼠标指针位置相对于当前窗口的 y 坐标,其中客户区域不包括窗口自 ...

  5. [翻译svg教程]Path元素 svg中最神奇的元素!

    先看一个实例 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999 ...

  6. jmobile学习之路 ----检测屏幕宽度

    <script type="text/javascript"> window.onresize = function(){ var myh1 = document.ge ...

  7. arm,iptables: No chain/target/match by that name.

    最近由于项目需要,需要打开防火墙功能. 公司有 arm linux 3.0x86 linux 3.2x86 linux 2.4 的三个嵌入式.都需要打开防火墙功能. 执行“whereis iptabl ...

  8. LeetCode题目按公司分类

    LinkedIn(39) 1 Two Sum 23.0% Easy 21 Merge Two Sorted Lists 35.4% Easy 23 Merge k Sorted Lists 23.3% ...

  9. pythonchallenge 解谜 Level 5

    第五关的确很坑爹... 不过,根据之前的思路,我想着是把信息放在了 “源码” 中. 翻了下源码.有用的东西在以下部分. <html><head> <title>pe ...

  10. C++迪杰斯特拉算法求最短路径

    一:算法历史 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以 ...