C# linq学习

using System;
using System.Linq;
using System.Collections.Generic;

namespace linqLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string[] Keywords = {
                "abstract", "add*", "alias*", "as", "ascending*",
                "async*", "await*", "base","bool", "break",
                "by*", "byte", "case", "catch", "char", "checked",
                "class", "const", "continue", "decimal", "default",
                "delegate", "descending*", "do", "double",
                "dynamic*", "else", "enum", "event", "equals*",
                "explicit", "extern", "false", "finally", "fixed",
                "from*", "float", "for", "foreach", "get*", "global*",
                "group*", "goto", "if", "implicit", "in", "int",
                "into*", "interface", "internal", "is", "lock", "long",
                "join*", "let*", "nameof*", "namespace", "new", "null",
                "object", "on*", "operator", "orderby*", "out",
                "override", "params", "partial*", "private", "protected",
                "public", "readonly", "ref", "remove*", "return", "sbyte",
                "sealed", "select*", "set*", "short", "sizeof",
                "stackalloc", "static", "string", "struct", "switch",
                "this", "throw", "true", "try", "typeof", "uint", "ulong",
                "unsafe", "ushort", "using", "value*", "var*", "virtual",
                "unchecked", "void", "volatile", "where*", "while", "yield*"
            };
            string[] Keywords2 = {
                "abstract", "add*", "alias*", "as", "ascending*",
                "async*", "await*", "base","bool", "break",
                "by*", "byte", "case", "catch", "char", "checked",
                "class", "const", "continue", "decimal", "default",
                "delegate", "descending*", "do", "double",
                "dynamic*" };

            IEnumerable<string> selection =
                (from word in Keywords
                 where !word.Contains('*')
                 select word).Distinct();                       //圆括号加distinct,去除重复
            Console.WriteLine("not contain *:");
            foreach(string keyword in selection)                
            {
                Console.Write(keyword + " ");
            }

            IEnumerable<int> selection2 =
                from word in Keywords
                where word.Length > 4
                select word.Length;
            Console.WriteLine("长字符串(长于4)的长度:");
            foreach(int len in selection2)
            {
                Console.Write(len+" ");
            }

            var firstAndLastChar =
                from word in Keywords
                let charArray = word.ToCharArray()                  //let字段用于增加代码的灵活性
                select new
                {
                    firstChar = charArray[0],
                    lastChar = charArray[word.Length - 1]
                };
            Console.WriteLine("开始和结束的char:");
            foreach (var charDui in firstAndLastChar) 
            {
                Console.Write(charDui.firstChar + " " + charDui.lastChar + "|");          
            }

            //查询表达式在这里,也就是真正遍历使用时才会去执行,所以所谓的查询表达式不过是一种筛选条件的存储
            //下面的验证结果就表明,其实查询表达式的执行结果和顺序和在for循环遍历Keywords,并在符合条件时才执行是一样的
            IEnumerable<string> validateDelay =
                from word in Keywords
                where isLenEnough(word)
                select word;
            Console.WriteLine("用于验证延迟表达式的执行顺序和原理:");
            foreach(string str in validateDelay)
            {
                Console.Write(str + " ");
            }           

            IEnumerable<string> descendingWords =
                from word in Keywords
                orderby word.ToCharArray()[0] descending,word.Length
                select word;
            Console.WriteLine("根据字符串首字母和长度排序:");
            foreach (string str in descendingWords)
            {
                Console.Write(str + " ");
            }

            IEnumerable<IGrouping<char, string>> groupSelection =
                from word in Keywords
                group word by word.ToCharArray()[0];
            Console.WriteLine("分组示范");
            foreach(IGrouping<char,string> wordGroup in groupSelection)
            {
                Console.WriteLine("\n"+wordGroup.Key+":");
                foreach(string str in wordGroup)
                {
                    Console.Write(str + " ");
                }
            }

            var groupIntoSelection =
                from word in Keywords
                group word by word.ToCharArray()[0]
                into groups
                select new                              //这里可以看出into 语句作用是比较弱的,只能用于一些逻辑上的延续而不能接下一个查询
                {
                    key = groups.Key,
                    contain = groups
                };

                
            Console.WriteLine("分组into示范");
            foreach (IGrouping<char, string> wordGroup in groupSelection)
            {
                Console.WriteLine("\n" + wordGroup.Key + ":");
                foreach (string str in wordGroup)
                {
                    Console.Write(str + " ");
                }
            }

            IEnumerable<char> twoFromStrings =
                from word in Keywords
                from chars in word
                select chars;

            IEnumerable<char> twoString =                                           
                from word in Keywords
                where word.Contains('*')
                from wordChar in word
                where wordChar < 'h'
                select wordChar;
            Console.WriteLine("使用并列From实现双重查询:");
            foreach(char item in twoString)
            {
                Console.Write(item+" ");
            }


            var dikaer =
                from string1 in Keywords
                from string2 in Keywords2
                select new { string1, string2 };

        }
        static bool isLenEnough(string str)
        {
            if (str.Length > 3)
            {
                Console.Write("#");
                return true;
            }
            else
            {
                return false;
            }
        }        
    }
}
发布了19 篇原创文章 · 获赞 2 · 访问量 5154

猜你喜欢

转载自blog.csdn.net/gunjiu4462/article/details/103845963