csharp 很少使用LINQ扩展方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 很少使用LINQ扩展方法相关的知识,希望对你有一定的参考价值。

// C#, LINQ

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace LINQ
{
    class LinqExtensionMethods
    {
        static void Main(string[] args)
        {
            AsParallel(); //Running the query as parallel. Slower for smaller collections.
            Distinct(); //Removes duplicate elements.
            Except(); //Except subtracts elements from a collection.
            GroupBy(); //GroupBy transforms a collection into groups. Each group has a key.
            Join(); //Joining matches every element in two collections based on some condition.
            Union(); //Unions removing duplicates.
            Zip(); //Combines two lists using a selector function
            OfType(); //Searches for elements by their types. 
            Intersect(); //Extracts the intersect.

            Console.Read();
        }

        private static void Intersect()
        {
            var list1 = new List<int> { 4, 3, 6 };
            var list2 = new List<int> { 5, 4, 2 };

            var enumerable = list1.Intersect(list2);
            enumerable.ToList().ForEach(Console.WriteLine);
        }

        private static void OfType()
        {
            var list = new List<object>()
            {
                new StringBuilder(),
                new object(),
                "example",
                3,
                42D,
                "string"
            };

            list.OfType<string>().ToList().ForEach(Console.WriteLine);
        }

        private static void Zip()
        {
            var list1 = new List<int> { 4, 3, 6 };
            var list2 = new List<int> { 5, 4, 2 };

            list1.Zip(list2, (l1, l2) => l1 + l2).ToList().ForEach(Console.WriteLine);
        }

        private static void Union()
        {
            var list1 = new List<int> { 4, 3, 6 };
            var list2 = new List<int> { 5, 4, 2 };

            list1.Union(list2).ToList().ForEach(Console.WriteLine);
        }

        private static void Join()
        {
            var list1 = new List<int> { 4, 3, 6 };
            var list2 = new List<int> { 5, 4, 2 };

            var result = from item1 in list1
                         join item2 in list2 on (item1 + 1) equals item2
                         select item1;

            var resultAlternative = list1.Join(list2, i1 => i1 - 2, i2 => i2 - 3, (i1, i2) => i2);

            result.ToList().ForEach(Console.WriteLine);
        }

        public static void GroupBy()
        {
            int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            var result = array.GroupBy(a => a % 2 == 0);
            result.ToList().ForEach(group =>
            {
                Console.Write(group.Key);
                group.ToList().ForEach(Console.WriteLine);
            });
        }

        private static void Except()
        {
            int[] values1 = { 1, 2, 3, 4 };
            int[] values2 = { 1, 2, 5 };

            var result = values1.Except(values2);

            result.ToList().ForEach(Console.WriteLine);
        }

        private static void Distinct()
        {
            var list = new List<int> { 1, 1, 2, 2, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 9, 9 };
            list.Distinct().ToList().ForEach(Console.WriteLine);
        }

        private static void AsParallel()
        {
            var list = Enumerable.Range(0, short.MaxValue).ToList();

            var s1 = Stopwatch.StartNew();
            for (int i = 0; i < 10000; i++)
            {
                var dummy = list.Sum();
            }
            s1.Stop();

            Console.WriteLine(s1.ElapsedMilliseconds);

            var s2 = Stopwatch.StartNew();
            for (int i = 0; i < 1000; i++)
            {
                var dummy = list.AsParallel().Sum();
            }
            s2.Stop();

            Console.WriteLine(s2.ElapsedMilliseconds);
        }
    }
}

以上是关于csharp 很少使用LINQ扩展方法的主要内容,如果未能解决你的问题,请参考以下文章

csharp 可枚举的LINQ扩展

linq 的查询语法和静态扩展的方法语法的对比

linq 的查询语法和静态扩展的方法语法的对比

linq 的查询语法和静态扩展的方法语法的对比

csharp 使用LINQ Project.To

csharp 在LINQ查询中使用AutoMapper