2.Linq实用性技巧篇

在论坛上经常会看到别人问,linq怎么实现递归,如何求笛卡尔积等问题。。都可以用linq快速方便的解决。。下面我就来个总的归纳

 

1.)递归

我们经常会遇到一个情况,就是想获取当前节点下的所有子节点。比如我们查询的时候,条件选的广东,那么广州、深圳、东莞都在该范围内,当然还包括广州下的街道等。我们首先想到的是递归来解决。但如何结合linq呢?下面来看看吧

     static List<City> list = new List<City>()
        {
            new City{id=1,name="广东",parentid=0},
            new City{id=2,name="广州",parentid=1},
            new City{id=3,name="深圳",parentid=1},
            new City{id=4,name="东莞",parentid=1},
            new City{id=5,name="越秀区",parentid=2},
            new City{id=6,name="白云区",parentid=2},
        };

        static void Main(string[] args)
        {
            var result = GetChilds(list.First());
        }

        public static IEnumerable<City> GetChilds(City city)
        {
            var temp = list.Where(x => x.parentid == city.id);
            return temp.Concat(temp.SelectMany(x => GetChilds(x)));
        }

很简单吧?这里我们可以就concat前后分2部份来看,temp为当前city的子节点temp.selectmany那里是找出该子节点下的子节点,这样就好理解了吧

 

2.)笛卡尔积

想到笛卡尔积,首先想到selectmany来处理

     static void Main(string[] args)
        {
            string[] s1 = { "A", "B", "C" };
            string[] s2 = { "D", "E" };
            var result = s1.SelectMany(x => s2.Select(y => x + y));
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }

 

结果如我们所想。但是如果是3个或4个或更多的数组呢?或许你想到循环,递归等。但这里我要给大家介绍一样东西,叫累加器-Aggregate。

 

static void Main(string[] args)
        {
            List<string[]> list = new List<string[]>();
            string[] s1 = { "A", "B", "C" };
            string[] s2 = { "D", "E" };
            string[] s3 = { "F", "G" };
            list.Add(s1);
            list.Add(s2);
            list.Add(s3);

            var result = list.Aggregate((thisCurrent, nextCurrent) => thisCurrent.SelectMany(x => nextCurrent.Select(y => x + y)).ToArray());
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }

运行结果

其实就是尾递归。

(thisCurrent, nextCurrent) => thisCurrent.SelectMany(x => nextCurrent.Select(y => x + y)).ToArray()
thisCurrent.SelectMany(x => nextCurrent.Select(y => x + y)).ToArray()会作为参数传到下次的thisCurrent中

 

3.)分组查询小介绍

linq的分组非常强大,用起来也非常爽。比如有一群人,我们想按他们的年龄进行分组,每10岁分一组,我们会这么做

 static void Main(string[] args)
        {
            List<int> list = new List<int>()
            {
                6,7,8,9,12,15,18,23,25,33,31,39,40
            };
            var result = list.GroupBy(x => x / 10);
            foreach (var item in result)
            {
                Console.WriteLine(string.Join(",", item));
            }
            Console.ReadLine();
        }

这里我就不截图了。很简单粗暴。

但如果我们想将1~18岁的归位一组(儿童组),18~28(青年组),29~40为(成人组)那我们该怎么办呢?还要加上个性别,分成女青年组 男青年组等,40岁以上的不分男女,统一归位老人组。这时大家怎么办呢?LINQ照样简单粗暴的解决问题

 

  public class Person
    {
        public string name { get; set; }
        public int age { get; set; }
        public string sex { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Person> list = new List<Person>()
            {
                new Person(){name="小A",age=5,sex=""},
                new Person(){name="小B",age=5,sex=""},
                new Person(){name="小C",age=7,sex=""},
                new Person(){name="小D",age=12,sex=""},
                new Person(){name="小E",age=20,sex=""},
                new Person(){name="小F",age=21,sex=""},
                new Person(){name="小G",age=25,sex=""},
                new Person(){name="小H",age=39,sex=""},
                new Person(){name="小I",age=55,sex=""},
                new Person(){name="小J",age=54,sex=""},
            };
            var result = list.GroupBy(x =>
            {
                if (x.age <= 18)
                    return x.sex + "儿童组";
                if (x.age > 18 && x.age <= 28)
                    return x.sex + "青年组";
                if (x.age > 28 && x.age <= 40)
                    return x.sex + "成人组";
                return "老人组";
            });

            foreach (var item in result)
            {
                string name = string.Join(",", item.Select(x => x.name));
                Console.WriteLine(string.Format("{0}:{1}", item.Key, name));
            }
            Console.ReadKey();
        }

运行结果

 

 

不知道大家学到东西没。。LINQ很灵活,关键就看你怎么玩了。。用得好的话一句话就能解决很多复杂的东西

转载于:https://www.cnblogs.com/irenebbkiss/p/4155719.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值