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

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

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

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

16.1查询表达式概述

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

        IEnumerable selection =
            from word in CSharp.Keywords
            where !word.Contains('*')
            select word;
16.1.1投射
        IEnumerable fileNames = Directory.GetFiles(
            rootDirectory, searchPattern);
 
        IEnumerable fileInfos =
            from fileName in fileNames
            select new FileInfo(fileName);
16.1.2筛选
        IEnumerable files =
            from fileName in Directory.EnumerateFiles(
                rootDirectory, searchPattern)
            where File.GetLastWriteTime(fileName) <
                DateTime.Now.AddMonths(-1)
            select new FileInfo(fileName);
16.1.3查询
        IEnumerable fileNames =
            from fileName in Directory.EnumerateFiles(
                rootDirectory, searchPattern)
            orderby (new FileInfo(fileName)).Length descending,
                fileName
            select fileName;
16.1.4let子句
        IEnumerable 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> selection =
            from word in CSharp.Keywords
            group word by word.Contains('*');
 
        foreach(IGrouping 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> keywordGroups =
            from word in CSharp.Keywords
            group word by word.Contains('*');
 
        IEnumerable<(bool IsContextualKeyword, 
            IGrouping Items)> selection =
            from groups in keywordGroups
            select
            (
                IsContextualKeyword: groups.Key,
                Items: groups
            );
 
        foreach (
            (bool isContextualKeyword, IGrouping 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 selection =
        from word in CSharp.Keywords
        where !word.Contains('*')
        select word;
    // ...
}

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

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

你可能感兴趣的:(C#学习笔记,c#,linq,开发语言,笔记,.net,学习)