LINQ练习

下面是自己做的一点LINQ语法的小练习,做一下备份以后来看。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQEX
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 8, 4, 5, 2, 1, 5, 6, 2 };
            var ans = arr.Where(p => p % 2 == 0).Select(p => p).OrderBy(p => p);
            foreach (int num in ans)
            {
                Debug.Write(num + " ");
            }
            Console.Read();
        }
    }
}

上面语句的作用是首先将arr数组里面偶数元素选出来,然后进行升序排序。

输出:2 2 4 6 8

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQEX
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] arr = new String[] { "Asd", "fdE", "RFd" };
            var ans = arr.Select(name => name.ToLower());
            foreach (String item in ans)
            {
                Debug.Write(item + " ");
            }
            Console.Read();
        }
    }
}

上面做了将arr数组里面的每一项都变为小写字母。

输出:asd  fde  rfd


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQEX
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] arr = new String[] { "Asd", "fdE", "RFd" };
            var ans = arr.Select(p=>p).Where(p=>p.ToLower()=="rfd");
            foreach (String item in ans)
            {
                Debug.Write(item + " ");
            }
            Console.Read();
        }
    }
}

上面做了从arr中筛选出小写字母为rfd的项。

输出:RFd


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQEX
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] arr = new String[] { "Asd", "fdE", "RFd", "As23d","R4dFd"};
            var ans = arr.GroupBy(s => s.Substring(0, 1));
            foreach (var group in ans)
            {
                foreach(var item in group)
                {
                    Debug.Write(item + " ");
                }
                Debug.WriteLine("");
            }
            Console.Read();
        }
    }
}

上面对arr按照首字母进行了分组操作(GroupBy)。然后输出每个分组里面每个字符串。

输出:

Asd As23d
fdE
RFd R4dFd


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQEX
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] arr = new String[] { "Asd", "fdE", "RFd", "As23d","R4dFd"};
            var ans = arr.GroupBy(s => s.Substring(0, 1)).Count();
            Debug.WriteLine(ans);
            //foreach (var group in ans)
            //{
            //    foreach(var item in group)
            //    {
            //        Debug.Write(item + " ");
            //    }
            //    Debug.WriteLine("");
            //}
            Console.Read();
        }
    }
}

上面作用是按照首字母分组然后计算分组数量。

输出:3  (因为有3组,分别是A , f R开头的三组)


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQEX
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 1,3,5,2,8,17};
            var ans = arr.Select(p => p).Max();
            Debug.WriteLine(ans);
            Console.Read();
        }
    }
}

上面输出arr里面最大的数字 17


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQEX
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 1,3,5,2,8,17};
            var ans = arr.Select(p => p).Average();
            Debug.WriteLine(ans);
            Console.Read();
        }
    }
}

上面输出arr的平均数 6


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQEX
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 1,3,5,2,8,17};
            var ans = arr.Select(p => p).Sum();
            Debug.WriteLine(ans);
            Console.Read();
        }
    }
}

上面输出arr数字的和 36


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQEX
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 1,3,5,2,8,17};
            var ans = arr.Skip(2).Take(3);        
            foreach( var item in ans)
            {
                Debug.WriteLine(item);
            }
            Console.Read();
        }
    }
}

上面例子用Skip跳过2个元素然后用Take取出3个元素。

输出:5  2  8


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQEX
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 1, 3, 3, 3, 3, 5, 2, 8, 17 };
            var ans = arr.Skip(1).Take(5).Distinct();
            foreach (var item in ans)
            {
                Debug.WriteLine(item);
            }
            Console.Read();
        }
    }
}

通过Diasinct()来将集合去重,最后输出 3 , 5


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQEX
{
    class Program
    {
        static void Main(string[] args)
        {
            var ans = Enumerable.Range(1, 10);
            foreach (var item in ans)
            {
                Debug.WriteLine(item);
            }
            Console.Read();
        }
    }
}

Range(s,t)方法是生成[s ,t]的整数的集合。

所以最后输出 1 2 3 4 5 6 7 8 9 10


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQEX
{
    class Program
    {
        static void Main(string[] args)
        {
            var ans = Enumerable.Repeat("hello", 3);
            foreach (var item in ans)
            {
                Debug.WriteLine(item);
            }
            Console.Read();
        }
    }
}

Repeat( i ,n)是生成n个i的函数。

所以最后输出 hello hello hello


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值