Where语句检查符合条件的元素,并返回新的迭代器

1:基于谓词筛选值序列

IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)

1.1:foreach解析:

            //IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)//foreach解析://source:TSource->stringList<string> fruits15 = new List<string> { "apple", "passionfruit", "banana", "mango","orange", "blueberry", "grape", "strawberry" };//predicate:TSource->itemConsole.WriteLine("基于谓词筛选值序列foreach解析:");foreach (string item in fruits15){if (item.StartsWith("a"))//predicate:bool->item.StartsWith("a"){Console.Write(item + ",");}}

1.2:原理图:执行方式:延迟流式执行

1.3:linq写法(查询语句):

            //2:linq写法(查询语句):var fruits16 = from f in fruits15.AsEnumerable()where f.StartsWith("a")select f;Console.WriteLine("基于谓词筛选值序列linq写法:");foreach (string item in fruits16){Console.WriteLine(item);}

1.4:查询方法写法:

            //3:查询方法写法:var fruits17 = fruits15.AsEnumerable().Where(f=> f.StartsWith("a"));Console.WriteLine("基于谓词筛选值序列查询方法写法:");foreach (string item in fruits17){Console.WriteLine(item);}

2:基于谓词筛选值序列。 将在谓词函数的逻辑中使用每个元素的索引。

IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)

2.1:foreach解析:

            //IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)//foreach解析://source:TSource->stringList<string> fruits25 = new List<string> { "apple", "passionfruit", "banana", "mango","orange", "blueberry", "grape", "strawberry" };int index = 0;//predicate:TSource->item//predicate:int->indexConsole.WriteLine("基于谓词筛选值序列foreach解析:");foreach (string item in fruits25){if (item.StartsWith("a"))//predicate:bool->item.StartsWith("a"){Console.Write(item + ",");}index++;}

2.2:原理解析::执行方式:延迟流式执行

2.2:linq写法(查询语句):没有

2.3:查询方法写法:

            //3:查询方法写法:var fruits27 = fruits25.AsEnumerable().Where((f, index25) => f.StartsWith("a") && index25 > 0);Console.WriteLine("基于谓词筛选值序列带索引查询方法写法:");foreach (string item in fruits27){Console.WriteLine(item);}

3:多层元素(类)的Where查询:

这里以搜索赛车手为例:

      /// <summary>/// 赛车手/// </summary>[Serializable]public class Racer : IComparable<Racer>, IFormattable{/// <summary>/// 名字/// </summary>public string FirstName { get; set; }/// <summary>/// 赛车手姓氏/// </summary>public string LastName { get; set; }/// <summary>/// 赛车手国家/// </summary>public string Country { get; set; }/// <summary>/// 夺冠次数/// </summary>public int Wins { get; set; }public int Starts { get; set; }/// <summary>/// 赛车手夺冠用的车名称/// </summary>public IEnumerable<string> Cars { get; private set; }/// <summary>/// 赛车手夺冠时的年份/// </summary>public IEnumerable<int> Years { get; private set; }public Racer(string firstName, string lastName, string country, int starts, int wins, IEnumerable<int> years, IEnumerable<string> cars){this.FirstName = firstName;this.LastName = lastName;this.Country = country;this.Starts = starts;this.Wins = wins;this.Years = new List<int>(years);this.Cars = new List<string>(cars);}public Racer(string firstName, string lastName, string country, int starts, int wins) : this(firstName, lastName, country, starts, wins, null, null){}public int CompareTo(Racer other){if (other == null) return -1;//升序//return string.Compare(this.LastName, other.LastName);//降序return string.Compare(other.LastName, this.LastName);}public string ToString(string format, IFormatProvider formatProvider){switch (format){case null:case "N":return ToString();case "F":return FirstName;case "L":return LastName;case "C":return Country;case "S":return Starts.ToString();case "W":return Wins.ToString();case "A":return String.Format("{0} {1}, {2}; starts: {3}, wins: {4}",FirstName, LastName, Country, Starts, Wins);default:throw new FormatException(String.Format("Format {0} not supported", format));}}public string ToString(string format){return ToString(format, null);}public override string ToString(){return String.Format("{0} {1}", FirstName, LastName);}}public static class Formula1{private static List<Racer> racers;/// <summary>/// 返回一组赛车手,包含了1950-2011年之间的所有一级方程式冠军/// </summary>/// <returns></returns>public static IList<Racer> GetChampions(){if (racers == null){racers = new List<Racer>(40);racers.Add(new Racer("Nino", "Farina", "Italy", 33, 5, new int[] { 1950 }, new string[] { "Alfa Romeo" }));racers.Add(new Racer("Alberto", "Ascari", "Italy", 32, 10, new int[] { 1952, 1953 }, new string[] { "Ferrari" }));racers.Add(new Racer("Juan Manuel", "Fangio", "Argentina", 51, 24, new int[] { 1951, 1954, 1955, 1956, 1957 }, new string[] { "Alfa Romeo", "Maserati", "Mercedes", "Ferrari" }));racers.Add(new Racer("Mike", "Hawthorn", "UK", 45, 3, new int[] { 1958 }, new string[] { "Ferrari" }));racers.Add(new Racer("Phil", "Hill", "USA", 48, 3, new int[] { 1961 }, new string[] { "Ferrari" }));racers.Add(new Racer("John", "Surtees", "UK", 111, 6, new int[] { 1964 }, new string[] { "Ferrari" }));racers.Add(new Racer("Jim", "Clark", "UK", 72, 25, new int[] { 1963, 1965 }, new string[] { "Lotus" }));racers.Add(new Racer("Jack", "Brabham", "Australia", 125, 14, new int[] { 1959, 1960, 1966 }, new string[] { "Cooper", "Brabham" }));racers.Add(new Racer("Denny", "Hulme", "New Zealand", 112, 8, new int[] { 1967 }, new string[] { "Brabham" }));racers.Add(new Racer("Graham", "Hill", "UK", 176, 14, new int[] { 1962, 1968 }, new string[] { "BRM", "Lotus" }));racers.Add(new Racer("Jochen", "Rindt", "Austria", 60, 6, new int[] { 1970 }, new string[] { "Lotus" }));racers.Add(new Racer("Jackie", "Stewart", "UK", 99, 27, new int[] { 1969, 1971, 1973 }, new string[] { "Matra", "Tyrrell" }));racers.Add(new Racer("Emerson", "Fittipaldi", "Brazil", 143, 14, new int[] { 1972, 1974 }, new string[] { "Lotus", "McLaren" }));racers.Add(new Racer("James", "Hunt", "UK", 91, 10, new int[] { 1976 }, new string[] { "McLaren" }));racers.Add(new Racer("Mario", "Andretti", "USA", 128, 12, new int[] { 1978 }, new string[] { "Lotus" }));racers.Add(new Racer("Jody", "Scheckter", "South Africa", 112, 10, new int[] { 1979 }, new string[] { "Ferrari" }));racers.Add(new Racer("Alan", "Jones", "Australia", 115, 12, new int[] { 1980 }, new string[] { "Williams" }));racers.Add(new Racer("Keke", "Rosberg", "Finland", 114, 5, new int[] { 1982 }, new string[] { "Williams" }));racers.Add(new Racer("Niki", "Lauda", "Austria", 173, 25, new int[] { 1975, 1977, 1984 }, new string[] { "Ferrari", "McLaren" }));racers.Add(new Racer("Nelson", "Piquet", "Brazil", 204, 23, new int[] { 1981, 1983, 1987 }, new string[] { "Brabham", "Williams" }));racers.Add(new Racer("Ayrton", "Senna", "Brazil", 161, 41, new int[] { 1988, 1990, 1991 }, new string[] { "McLaren" }));racers.Add(new Racer("Nigel", "Mansell", "UK", 187, 31, new int[] { 1992 }, new string[] { "Williams" }));racers.Add(new Racer("Alain", "Prost", "France", 197, 51, new int[] { 1985, 1986, 1989, 1993 }, new string[] { "McLaren", "Williams" }));racers.Add(new Racer("Damon", "Hill", "UK", 114, 22, new int[] { 1996 }, new string[] { "Williams" }));racers.Add(new Racer("Jacques", "Villeneuve", "Canada", 165, 11, new int[] { 1997 }, new string[] { "Williams" }));racers.Add(new Racer("Mika", "Hakkinen", "Finland", 160, 20, new int[] { 1998, 1999 }, new string[] { "McLaren" }));racers.Add(new Racer("Michael", "Schumacher", "Germany", 287, 91, new int[] { 1994, 1995, 2000, 2001, 2002, 2003, 2004 }, new string[] { "Benetton", "Ferrari" }));racers.Add(new Racer("Fernando", "Alonso", "Spain", 177, 27, new int[] { 2005, 2006 }, new string[] { "Renault" }));racers.Add(new Racer("Kimi", "Räikkönen", "Finland", 148, 17, new int[] { 2007 }, new string[] { "Ferrari" }));racers.Add(new Racer("Lewis", "Hamilton", "UK", 90, 17, new int[] { 2008 }, new string[] { "McLaren" }));racers.Add(new Racer("Jenson", "Button", "UK", 208, 12, new int[] { 2009 }, new string[] { "Brawn GP" }));racers.Add(new Racer("Sebastian", "Vettel", "Germany", 81, 21, new int[] { 2010, 2011 }, new string[] { "Red Bull Racing" }));}return racers;}
}

示例:这里以1.1的方法变体查询

找出至少赢得15场比赛的巴西和奥地利赛车手

3.1:foreach解析:

            //IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)//foreach解析://source:TSource->stringvar gc1 = Formula1.GetChampions();//predicate:TSource->itemforeach (var r in gc1){//bool的判断使用lambda表达式//predicate:bool->r.Wins >= 15 && (r.Country == "Brazil" || r.Country == "Austria")if (r.Wins >= 15 && (r.Country == "Brazil" || r.Country == "Austria")){Console.WriteLine("2.1.1:{0:A}", r);}}

3.2:linq写法(查询语句):

            //2.1:找出至少赢得15场比赛的巴西和奥地利赛车手//2.1.1:语法糖var racers = from r in Formula1.GetChampions()where r.Wins >= 15 && (r.Country == "Brazil" || r.Country == "Austria")select r;foreach (Racer r in racers){Console.WriteLine("2.1.1:{0:A}", r);}

3.3:查询方法写法:

            //写法2:2.1.3var racers3 = Formula1.GetChampions().Where(r => r.Wins >= 15 && (r.Country == "Brazil" || r.Country == "Austria")).Select(r => r);//与写法2一样,select方法可以舍弃var racers35 = Formula1.GetChampions().Where(r => r.Wins >= 15 && (r.Country == "Brazil" || r.Country == "Austria"));foreach (Racer r in racers3){Console.WriteLine("2.1.3:{0:A}", r);}

C#-linq实战003-查询-Where相关推荐

  1. 《LINQ实战》译者感言

    < LINQ 实战>译者感言 此刻您手中这本沉甸甸的书,虽然来得晚了一些,但仍旧是不可多得 LINQ 权威学习指南. 在最近一年的工作中,我会经常用到 LINQ .毫不夸张地说,我无法想象 ...

  2. springboot controller 分页查询_Spring Boot实战分页查询附近的人:Redis+GeoHash+Lua

    前言 最近在做社交的业务,用户进入首页后需要查询附近的人: 项目状况:前期尝试业务阶段: 特点: 快速实现(不需要做太重,满足初期推广运营即可) 快速投入市场去运营 收集用户的经纬度: 用户在每次启动 ...

  3. LINQ 的标准查询操作符

    摘自msdn (http://msdn.microsoft.com/zh-cn/magazine/cc337893.aspx) LINQ 的标准查询操作符 John Papa 代码下载位置: Data ...

  4. LINQ – 使用DataLoadOptions 提高LINQ to SQL 查询性能

    LINQ – 使用DataLoadOptions 提高LINQ to SQL 查询性能 EntLib.com开源小组发表,http://www.EntLib.com,2008-7-2 LINQ to ...

  5. C# 将控件的Controls属性下的所有控件取出以便使用LINQ语句进行查询

    /// <summary> /// 将容器的Contro属性下控件的转换成枚举类型以便使用LINQ语句进行查询/// </summary>/// <param name= ...

  6. Entity Framework 4 in Action读书笔记——第四章:使用LINQ to Entities查询:使用函数...

    4.7 使用函数 扩展LINQ to Entities查询的简便方法就是使用函数.有四种类型的函数可以应用: 规范函数-LINQ to Entities本身没有提供的一组预定义的函数. 数据库函数-一 ...

  7. springboot controller 分页查询_Spring Boot实战分页查询附近的人: Redis+GeoHash+Lua

    您的支持是我不断创作巨大动力 CSDN博客地址(关注,点赞) 人工智能推荐 GitHub(Star,Fork,Watch) 前言 最近在做社交的业务,用户进入首页后需要查询附近的人: 项目状况:前期尝 ...

  8. Elasticsearch实战——地理位置查询

    Elasticsearch实战--地理位置查询 文章目录 Elasticsearch实战--地理位置查询 1. 半径查询(geo_distance query) 2. 指定矩形内的查询(geo_bou ...

  9. Linq语句集成查询

    开发工具与关键技术: MVC 撰写时间:2021/6/18 Linq to SQL 全称基于关系数据的.NET 语言集成查询,用于以对象形式管理关系数据,我们常用Linq语句去查询SQL内的数据,可以 ...

  10. 微信小程序实战—快递查询

    微信小程序实战-快递查询案例 需求:输入快递单号,点击查询按钮即可查看快递信息 api:阿里云全国快递物流查询 关键代码: 数据定义(index.js) data: {motto: 'Hello Wo ...

最新文章

  1. Spring Data JPA单元测试 Not a managed type
  2. 集合竞价如何买入_集合竞价的那些事:开盘涨停,这样做你也能抢到!
  3. Node中自启动工具supervisor的使用
  4. SQL:给查询添加一个合计行
  5. 修改 MySQL 自增ID的起始值
  6. uva 10954——Add All
  7. Python3——类
  8. lwip协议栈在linux运行,LwIP协议栈在uCOS II下的实现
  9. 稿定设计怎么免费去水印
  10. ajxs跨域 php_PHP Ajax 跨域问题最佳解决方案
  11. 使用yum update更新文件系统时不更新内核的方法
  12. 【2019百度之星初赛三1002=HDU6714】最短路 2(spfa+思维)
  13. 【Linux】U盘配合WinPE 安装debian8.6
  14. RNA-seq流程学习笔记(5)-Linux系统下载UCSC人类基因组和基因注释文件(未完成)
  15. unity3d开发AR/VR应用
  16. 贪心入门+10道例题+解析代码
  17. barbie黄佳丽--华侨大学
  18. 盘点10款超好用的数据可视化工具
  19. 解决谷歌无法加载扩展程序
  20. 某Q音乐最新歌曲查询API 可用!

热门文章

  1. inter cpu 测试软件,Intel发布新版官方CPU检测工具支持64系统
  2. 华为手机录屏大师录制的视频在本地目录找不到无法转移到PC的问题解决方案——免费转移视频
  3. Apollo MPC OSQP Solver
  4. 【SpringBoot】:springboot整合FTP文件上传与下载功能
  5. python调用按键精灵插件_【师兄带你学Python-1】你会涮火锅吗?
  6. 【应用篇】MyBatis学习笔记
  7. hp M1005 激光打印机 通过usb接入路由器 变成网络打印机
  8. html5显示状态灯,如何使用css3+html5来制作文字霓虹灯效果
  9. 耳机接口规则_耳机接口种类与标准
  10. 字号与磅值对应关系_终极版式指南:磅值,大写与小写,Em和En破折号等