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

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

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

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

16.1查询表达式概述

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

        IEnumerable<string> selection =
            from word in CSharp.Keywords
            where !word.Contains('*')
            select word;
16.1.1投射
        IEnumerable<string> fileNames = Directory.GetFiles(
            rootDirectory, searchPattern);
 
        IEnumerable<FileInfo> fileInfos =
            from fileName in fileNames
            select 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,
                fileName
            select fileName;
16.1.4let子句
        IEnumerable<FileInfo> files =
            from fileName in Directory.EnumerateFiles(
                rootDirectory, searchPattern)
            orderby new FileInfo(fileName).Length, fileName
            select new FileInfo(fileName);
16.1.5分组
    private static void GroupKeywords1()
    {
        IEnumerable<IGrouping<bool, string>> selection =
            from word in CSharp.Keywords
            group word by word.Contains('*');
 
        foreach(IGrouping<bool, string> wordGroup
            in 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.Keywords
            group word by word.Contains('*');
 
        IEnumerable<(bool IsContextualKeyword, 
            IGrouping<bool, string> Items)> selection =
            from groups in keywordGroups
            select
            (
                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.Keywords
    from character in word
    select character;

var numbers = new[] { 1, 2, 3 };
IEnumerable<(string Word, int Number)> product =
     from word in CSharp.Keywords
     from number in numbers
     select (word, number);

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

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

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

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

private static void ShowContextualKeywords2()
{
    IEnumerable<string> selection =
        CSharp.Keywords.Where(word => !word.Contains('*'));
    // ...
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值