Linq不经常使用但必须记住的用法

经常在项目中为了完成某个特定的功能需要去写很多代码,后来走读书籍,才发现真的不必如此,所遇到的,微软已经为你做了,除了那些常用的linq语法外,掌握下面这些不是特别常用的,完成工作将变得更加得心应手:

条目:

1.匿名方法
2.OfType用法
3.AsEnumerable用法
4.柯里化(Currying)表达式
5.匿名类型的相等性
6.次要排序
7.Intersect扩展方法
8.Union扩展方法
9.Except扩展方法
10.Group多字段分组
11.实现左外联接

测试用例:

    

class Program
    {
        static void Main(string[] args)
        {
            string split = new String('-', 30);
            Console.WriteLine("匿名方法");
            Console.WriteLine(split);
            Func<int, int, int> func = delegate(int a, int b)
            {
                return a + b;
            };
            Console.WriteLine(func(1, 2));
            Console.WriteLine("");
            Console.WriteLine("OfType用法");
            Console.WriteLine(split);
            var arr = new object[] { 1232, "1231", 1231, "asdac" };
            foreach (var value in arr.OfType<string>())
            {
                Console.WriteLine(value);
            }
            Console.WriteLine("");
            Console.WriteLine("AsEnumerable用法");
            Console.WriteLine(split);
            Console.WriteLine("如果某个集合对象本身实现了Where方法,则可以obj.AsEnumerable().Where()方式进行调用,在某些场合下,这非常有用!");
            Console.WriteLine("");
            Console.WriteLine("ToLookup用法");
            Console.WriteLine(split);
            var lookups = (new[]{
                    new {id=1,name="name1"},
                    new {id=1,name="name2"},
                    new {id=2,name="name3"},
            }).ToLookup(o => o.id, o => o.name);
            foreach (var lookup in lookups)
            {
                Console.WriteLine("\b\b" + lookup.Key);
                foreach (var value in lookup)
                    Console.WriteLine(value);
            }
            Console.WriteLine("");
            Console.WriteLine("柯里化(Currying)表达式");
            Console.WriteLine(split);
            Func<int, Func<int, int>> curry1 = x => y => x + y;
            Console.WriteLine(curry1(3)(4));
            Console.WriteLine("理解:x=>y=>x+y 即在第一个匿名函数里面再封装一个匿名函数,在第二个匿名函数里,x可理解为闭包");
            Console.WriteLine("");
            Console.WriteLine("匿名类型的相等性");
            Console.WriteLine(split);
            var eq1 = new { id = 1, name = "name" };
            var eq2 = new { id = 1, name = "name" };
            var eq3 = new { name = "name", id = 1 };
            Console.WriteLine("eq1.Equals(eq2)=" + eq1.Equals(eq2));
            Console.WriteLine("eq1.Equals(eq3)=" + eq1.Equals(eq3));
            Console.WriteLine("eq1 == eq2 =" + (eq1 == eq2));
            Console.WriteLine("");
            Console.WriteLine("次要排序");
            Console.WriteLine(split);
            var orderObj = new[]{
                new {lastname="Kimmel",first="Paul",age=41},
                new {lastname="Swanson",first="Dena",age=26},
                new {lastname="Swanson",first="Joe",age=4},
                new {lastname="Kimmel",first="Noah",age=11}
            };
            var sordid = from order in orderObj
                         orderby order.lastname, order.age
                         select order;
            foreach (var o in sordid)
            {
                Console.WriteLine("lastname:{0},first:{1},age:{2}", o.lastname, o.first, o.age);
            }
            Console.WriteLine("");
            Console.WriteLine("Intersect扩展方法");
            Console.WriteLine(split);
            var intersect1 = new int[] { 1, 2, 32, 3, 4, 5, 65 };
            var intersect2 = new int[] { 1, 2, 3, 345, 6, 5, 65 };
            Array.ForEach<int>(intersect1.Intersect(intersect2).ToArray(), e => Console.WriteLine(e + ","));
            Console.WriteLine("");
            Console.WriteLine("Union扩展方法");
            Console.WriteLine(split);
            var union1 = new int[] { 1, 2, 32, 3, 4, 5, 65 };
            var union2 = new int[] { 1, 2, 3, 345, 6, 5, 65 };
            Array.ForEach<int>(union1.Union(union2).ToArray(), e => Console.WriteLine(e + ","));
            Console.WriteLine("");
            Console.WriteLine("Except扩展方法");
            Console.WriteLine(split);
            var except1 = new int[] { 1, 2, 32, 3, 4, 5, 65 };
            var except2 = new int[] { 1, 2, 3, 345, 6, 5, 65 };
            Array.ForEach<int>(except1.Except(except2).ToArray(), e => Console.WriteLine(e + ","));
            Console.WriteLine("");
            Console.WriteLine("Group多字段分组");
            Console.WriteLine(split);
            var groups = (new[]{
                new {name="name1",age=18},
                new {name="name2",age=18},
                new {name="name4",age=19}
            }).GroupBy(o=>new{o.age,o.name});
            foreach (var group in groups)
            {
                Console.WriteLine("key.age:{0},key.name:{1},count:{2}", group.Key.age, group.Key.name, group.Count());
            }
            Console.WriteLine("");
            Console.WriteLine("实现左外联接");
            Console.WriteLine(split);
            var customers = new Customer[]{
                        new Customer(){ CompanyName="company1", ID=1},
                        new Customer(){ CompanyName="company2", ID=2},
                        new Customer(){ CompanyName="company3", ID=3},
                        new Customer(){ CompanyName="company4", ID=4},
                        new Customer(){ CompanyName="company5", ID=5},
            };
            var orders = new Order[]{
                new Order(){ CompanyId=1, ID=1, ItemDescription="this is order 1"},
                new Order(){ CompanyId=1,ID =2,ItemDescription="this is order 2"}
            };
 
            var joinGroup = from customer in customers
                        join order in orders on customer.ID equals order.CompanyId
                        into orderCollection
                        from item in orderCollection.DefaultIfEmpty(new Order())
                        select new { customer, item };
            foreach (var g in joinGroup)
            {
                Console.WriteLine("companyname:{0},companyid:{1},itemdescription:{2}", g.customer.CompanyName, g.customer.ID, g.item.ItemDescription);
            }                                                                                                                                                           
            Console.Read();
        }
        public class Customer
        {
            public int ID;
            public string CompanyName;
        }
        public class Order
        {
            public int ID;
            public int CompanyId;
            public string ItemDescription;
        }
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值