您现在的位置: 365建站网 > 365文章 > 常用Lambda范例

常用Lambda范例

文章来源:365jz.com     点击数:257    更新时间:2009-09-14 10:44   参与评论

1.用Where()方法进行筛选

using System;
using System.Linq;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intary = { 1, 2, 3, 4, 5, 6, 7, 8, 89, 10 };
            //查询所有能被2整除的元素
            var query1 = intary.Where(num => num % 2 == 0);
            Console.WriteLine("Query1:");
            foreach (var item in query1)
            {
                Console.WriteLine("{0}", item);
            }

            //查询所有值大于3被索引的元素
            var query2 = intary.Where((num, index) => num > index * 3);
            Console.WriteLine("Query2:");
            foreach (var item in query2)
            {
                Console.WriteLine("{0}", item);
            }
        }
    }
}

2.用OrderBy()方法进行排序

using System;
using System.Linq;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intary = { 1, 2, 3, 4, 5, 6, 7, 8, 89, 10 };
            //对所有元素按照%10进行升序排列,这是默认的排序方式
            var query1 = intary.OrderBy(val => val % 10 );
            Console.WriteLine("Query1:");
            foreach (var item in query1)
            {
                Console.WriteLine("{0}", item);
            }

            //对所有元素按照%10进行降序排列
            var query2 = intary.OrderByDescending(val => val % 10);
            Console.WriteLine("Query2:");
            foreach (var item in query2)
            {
                Console.WriteLine("{0}", item);
            }
        }
    }
}

以上的两个排序Demo都是采用默认的int排序比较器,在一些开发中,以下两种情况需要使用特定的数据比较器:

  • 默认的数据类型不能满足特殊的比较需要
  • 自定义的类不存在默认的数据比较器

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

namespace ConsoleApplication3
{
    //自定义int类型比较器,实现IComparable<int>接口
    class MyCompare : IComparer<int>
    {
        public int Compare(int x, int y)
        {
            int x1 = Math.Abs(x);
            int y1 = Math.Abs(y);
            if (x1 > y1)
            {
                return 1;
            }
            else if (x1 == y1)
            {
                return 0;
            }
            else
            {
                return -1;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //实例化自定义比较器
            MyCompare mc = new MyCompare();
            int[] intary = { 1, -2, 3, -4, 5, -6, 7, -8, 9, -10 };
            var query1 = intary.OrderBy(val => val, mc);
            Console.WriteLine("Query1");
            foreach (var item in query1)
            {
                Console.WriteLine("{0}",item);
            }
        }
    }
}

3.用Skip()、SkipWhile()跳过元素

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intary = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40 };
            //跳过intAry中前3个元素
            var query1 = intary.Skip(3);
            Console.WriteLine("Query1");
            foreach (var item in query1)
            {
                Console.WriteLine("{0}", item);
            }

            //跳过intAry中小于5的元素
            var query2 = intary.SkipWhile(num => num < 5);
            Console.WriteLine("Query2");
            foreach (var item in query2)
            {
                Console.WriteLine("{0}", item);
            }
        }
    }
}


4.用Take()、TakeWhile()提取元素

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intary = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40 };
            //取intAry中前3个元素
            var query1 = intary.Take(3);
            Console.WriteLine("Query1");
            foreach (var item in query1)
            {
                Console.WriteLine("{0}", item);
            }

            //取intAry中小于5的元素
            var query2 = intary.TakeWhile(num => num < 5);
            Console.WriteLine("Query2");
            foreach (var item in query2)
            {
                Console.WriteLine("{0}", item);
            }
        }
    }
}

5.对元素进行数值计算

  • Min():求最小值
  • Max():求最大值
  • Sum():求和
  • Average():求平均值

using System;
using System.Linq;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intary = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40 };
            var intMin = intary.Min();
            var intMax = intary.Max();
            var intSum = intary.Sum();
            var intAverage = intary.Average();
            Console.WriteLine("intary's min={0},max={1},sum={2},average={3}",intMin,intMax,intSum,intAverage);

            string[] strAry = { "Kevin", "James", "Ken", "Ben" };
            var strMin = strAry.Min();
            var strMax = strAry.Max();
            Console.WriteLine("strAry's Min = {0},Max={1}",strMin,strMax);
        }
    }
}

6.用Distinct()消除集合中相等的元素

 using System;
using System.Linq;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intary = { 1,1, 2,2, 3, 3, 4, 5};
            var query1 = intary.Distinct();
            foreach (var item in query1)
            {
                Console.WriteLine(item);
            }
        }
    }
}

7.用Concat()连接两个集合

using System;
using System.Linq;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strAry1 = {"Kevin","James","Ben"};
            string[] strAry2 = { "Ken","Joanna","cc"};
            var query1 = strAry1.Concat(strAry2);
            Console.WriteLine("Query1");
            foreach (var item in query1)
            {
                Console.WriteLine(item);
            }

            var query2 = strAry2.Concat(strAry1);
            Console.WriteLine("Query2");
            foreach (var item in query2)
            {
                Console.WriteLine(item);
            }
        }
    }
}

注意:Concat()方法是直接将两个集合中的元素连接在一起,不会进行重新排序、过滤等,就算两个集合中元素有重复也同样保留。

8.集合操作

说到集合,很容易想起并集、交集、差集3种常用操作。在LINQ中,IEnumerable<T>类分别通过Union()、Intersect()、Except完成这3种操作()。

using System;
using System.Linq;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intAry1 = {1,2,3};
            int[] intAry2 = {3,4,5};
            var query1 = intAry1.Union(intAry2);
            Console.WriteLine("并集");
            foreach (var item in query1)
            {
                Console.WriteLine(item);
            }

            var query2 = intAry1.Intersect(intAry2);
            Console.WriteLine("交集");
            foreach (var item in query2)
            {
                Console.WriteLine(item);
            }


Untitled Page 作者:深山老林
出处:http://wlb.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (257人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号