当前位置: 首页 > news >正文

C#8.0本质论第十六章--使用查询表达式的LINQ

C#8.0本质论第十六章–使用查询表达式的LINQ

像SQL这样的专业查询语言虽然容易阅读和理解,但又缺乏C#语言的完整功能。这正是C#语言设计者在C#3.0中添加查询表达式语法的原因。

本章大部分都类似于SQL,一般不会使用到,在用到的时候再去书里查吧。

16.1查询表达式概述

开发者经常对集合进行赛选来删除不想要的项,以及对集合进行投射将其中的项变成其它形式。

        IEnumerable<string> selection =from word in CSharp.Keywordswhere !word.Contains('*')select word;
16.1.1投射
        IEnumerable<string> fileNames = Directory.GetFiles(rootDirectory, searchPattern);IEnumerable<FileInfo> fileInfos =from fileName in fileNamesselect new FileInfo(fileName);
16.1.2筛选
        IEnumerable<FileInfo> files =from fileName in Directory.EnumerateFiles(rootDirectory, searchPattern)where File.GetLastWriteTime(fileName) <DateTime.Now.AddMonths(-1)select new FileInfo(fileName);
16.1.3查询
        IEnumerable<string> fileNames =from fileName in Directory.EnumerateFiles(rootDirectory, searchPattern)orderby (new FileInfo(fileName)).Length descending,fileNameselect fileName;
16.1.4let子句
        IEnumerable<FileInfo> files =from fileName in Directory.EnumerateFiles(rootDirectory, searchPattern)orderby new FileInfo(fileName).Length, fileNameselect new FileInfo(fileName);
16.1.5分组
    private static void GroupKeywords1(){IEnumerable<IGrouping<bool, string>> selection =from word in CSharp.Keywordsgroup word by word.Contains('*');foreach(IGrouping<bool, string> wordGroupin selection){Console.WriteLine(Environment.NewLine + "{0}:",wordGroup.Key ?"Contextual Keywords" : "Keywords");foreach(string keyword in wordGroup){Console.Write(" " +(wordGroup.Key ?keyword.Replace("*", null) : keyword));}}}
16.1.6使用into实现查询延续
    private static void GroupKeywords1(){IEnumerable<IGrouping<bool, string>> keywordGroups =from word in CSharp.Keywordsgroup word by word.Contains('*');IEnumerable<(bool IsContextualKeyword, IGrouping<bool, string> Items)> selection =from groups in keywordGroupsselect(IsContextualKeyword: groups.Key,Items: groups);foreach ((bool isContextualKeyword, IGrouping<bool, string> items)in selection){Console.WriteLine(Environment.NewLine + "{0}:",isContextualKeyword ?"Contextual Keywords" : "Keywords");foreach (string keyword in items){Console.Write(" " + keyword.Replace("*", null));}}}
16.1.7用多个from子句"平整"序列的序列
var selection =from word in CSharp.Keywordsfrom character in wordselect character;var numbers = new[] { 1, 2, 3 };
IEnumerable<(string Word, int Number)> product =from word in CSharp.Keywordsfrom number in numbersselect (word, number);

16.2查询表达式只是方法调用

令人惊讶的是,在C#3.0中引入查询表达式并未对CLR或者CIL进行任何改动。相反,编译器只是将查询表达式转换成一系列方法调用。

private static void ShowContextualKeywords1()
{IEnumerable<string> selection =from word in CSharp.Keywordswhere !word.Contains('*')select word;// ...
}

编译之后,会转换成一个由System.Linq.Enumerable提供的IEnumerable扩展方法调用。

private static void ShowContextualKeywords2()
{IEnumerable<string> selection =CSharp.Keywords.Where(word => !word.Contains('*'));// ...
}

相关文章:

  • 强推六款满分AI写作工具,需要自取
  • 输出SearchFacesResponse对象的JSON格式字符串回包乱码解决方案
  • 21、Resnet50 中包含哪些算法?
  • vite的使用
  • 开启gitlab中远程连接pgsql
  • 【Python-随笔】使用Python实现屏幕截图
  • Java研学-反射与内省
  • WebGL笔记:js中矩阵库的使用
  • linux常用命令-find命令与scp命令详解(超详细)
  • Qt Rsa 加解密方法使用(pkcs1, pkcs8, 以及文件存储和内存存储密钥)
  • tomcat运行项目时,前端页面中文乱码
  • 强化学习------时序差分(Temporal-Difference Learning)
  • Failed building wheel for opencv-python which use PEP 517
  • navicat某些表为什么不按主键排序
  • 力扣题:字符串的反转-11.21
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 「前端早读君006」移动开发必备:那些玩转H5的小技巧
  • co.js - 让异步代码同步化
  • javascript 哈希表
  • LintCode 31. partitionArray 数组划分
  • SpringBoot 实战 (三) | 配置文件详解
  • Vue2 SSR 的优化之旅
  • 关于Flux,Vuex,Redux的思考
  • 类orAPI - 收藏集 - 掘金
  • 我感觉这是史上最牛的防sql注入方法类
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • 原生Ajax
  • HanLP分词命名实体提取详解
  • 回归生活:清理微信公众号
  • ​Python 3 新特性:类型注解
  • ​学习一下,什么是预包装食品?​
  • ![CDATA[ ]] 是什么东东
  • #快捷键# 大学四年我常用的软件快捷键大全,教你成为电脑高手!!
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (16)Reactor的测试——响应式Spring的道法术器
  • (C语言)字符分类函数
  • (编译到47%失败)to be deleted
  • (二)正点原子I.MX6ULL u-boot移植
  • (附源码)php投票系统 毕业设计 121500
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (学习日记)2024.01.09
  • (一)Linux+Windows下安装ffmpeg
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (转载)Google Chrome调试JS
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • .mkp勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET 设计一套高性能的弱事件机制
  • .NET/C# 阻止屏幕关闭,阻止系统进入睡眠状态
  • .NET国产化改造探索(三)、银河麒麟安装.NET 8环境
  • .NET开源的一个小而快并且功能强大的 Windows 动态桌面软件 - DreamScene2
  • [].slice.call()将类数组转化为真正的数组
  • [AR Foundation] 人脸检测的流程
  • [Bada开发]初步入口函数介绍