博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LINQ系列:Linq to Object限制操作符
阅读量:6898 次
发布时间:2019-06-27

本文共 4056 字,大约阅读时间需要 13 分钟。

1. Where

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

1.1 原型定义

public static IEnumerable
Where
(this IEnumerable
source, Func
predicate);
public static IEnumerable
Where
(this IEnumerable
source, Func
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
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[] { 1, 1, 2, 3, 5, 8, 13, 21 };IEnumerable
query = Enumerable.Where(fibonacci, f => f > 5);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[] { 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);}

1.5 自定义实现

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

  C#2.0实现方式:

public static IEnumerable
Where
(this IEnumerable
source, Func
predicate);
using System;using System.Collections.Generic;using System.Text;namespace Northwind.CustomLINQExtension{    public static class CustomLINQExtension    {        public delegate TResult Func
(T arg); public static IEnumerable
Where
(IEnumerable
source, Func
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[] { 1, 1, 2, 3, 5, 8, 13, 21 };            IEnumerable
expr = CustomLINQExtension.Where(fibonacci, (delegate(int i) { return i > 3; })); 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
(T arg); public static IEnumerable
Where
(this IEnumerable
source, Func
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[] { 1, 1, 2, 3, 5, 8, 13, 21 };            IEnumerable
expr = fibonacci.Where(delegate(int i) { return i > 3; }); foreach (int item in expr) { Console.WriteLine(item); } } }}

转载于:https://www.cnblogs.com/libingql/p/4040806.html

你可能感兴趣的文章
WPF异常捕获三种处理 UI线程, 全局异常,Task异常
查看>>
分布式之redis精讲
查看>>
小命令大作用---Linux 下快速查找
查看>>
如何理解 koa 中间件执行机制
查看>>
关于“深入浅出 React Native:使用 JavaScript 构建原生应用”
查看>>
Linux启动过程学习
查看>>
【linux+C】神器 vim + 指针相关客串
查看>>
华为 21 级程序员月薪曝光: 270k 封神! 众网友直呼长见识
查看>>
裸辞后,从Android转战Web前端的学习以及求职之路
查看>>
Makefile的常用技术总结
查看>>
java时间工具 判断时间大于一个月,小于一年,时间必须以月为单位分割(欢迎测试)...
查看>>
轻松搞定RabbitMQ开篇:Java消息队列与JMS的诞生
查看>>
MySQL:MGR 学习(2):Write set(写集合)的写入过程
查看>>
Docker+Selenium Grid构建分布式Web测试环境
查看>>
操作系统复习题-第七章 中断和信号机构
查看>>
snakemake--我最喜欢的流程管理工具
查看>>
如何用 Python 和 gensim 调用中文词嵌入预训练模型?
查看>>
nginx三种安装方式
查看>>
陷阱:千万不要随便把serlvet.jar之类的包放在系统的classpath下面
查看>>
K8S有状态服务-云盘扩容解决方案
查看>>