当前位置: 移动技术网 > IT编程>开发语言>c# > Linq下有一个非常实用的SelectMany方法,很多人却不会用

Linq下有一个非常实用的SelectMany方法,很多人却不会用

2020年04月14日  | 移动技术网IT编程  | 我要评论

在平时开发中经常会看到有些朋友或者同事在写代码时会充斥着各种for,foreach,这种程式代码太多的话阅读性特别差,而且还显得特别累赘,其实在fcl中有很多帮助我们提高阅读感的方法,而现实中很多人不会用或者说不知道,这篇我就跟大家聊一聊。

一:selectmany

这个方法绝对是提高开发速度的一大利器,有太多的业务场景需要使用这个函数,举一个我实际应用场景,商家按照年份和客户类型预先设置一些标签,然后让系统跑一下它的各自标签到底有多少人?

1. 定义model

为了方便演示,这里做了一下简化代码,只有一个字典,key表示年份,value:就是该年份的多组标签。

    public class estimatemodel
    {
        public int shopid { get; set; }

        //key: 年份
        public dictionary<string, list<tagcrowdfiltermodel>> yearcrowdfilterdict { get; set; }
    }

    public class tagcrowdfiltermodel
    {
        /// <summary>
        /// 筛选条件
        /// </summary>
        public string crowdfiter { get; set; }

        /// <summary>
        /// 获取人数
        /// </summary>
        public int totalcustomercount { get; set; }
    }

为了更加清晰,我决定再填充一下数据

        public static void main(string[] args)
        {
            var estimatemodel = new estimatemodel()
            {
                shopid = 1,
                yearcrowdfilterdict = new dictionary<string, list<tagcrowdfiltermodel>>()
                {
                    {
                        "17年",new list<tagcrowdfiltermodel>()
                               {
                                 new tagcrowdfiltermodel(){ crowdfiter="between 10 and 20" },
                                 new tagcrowdfiltermodel(){ crowdfiter=" a<10  || a>30" },
                               }
                    },
                    {
                        "18年",new list<tagcrowdfiltermodel>()
                               {
                                 new tagcrowdfiltermodel(){ crowdfiter="between 100 and 200" },
                                 new tagcrowdfiltermodel(){ crowdfiter=" a<100  || a>300" },
                               }
                    },
                    {
                        "19年",new list<tagcrowdfiltermodel>()
                               {
                                 new tagcrowdfiltermodel(){ crowdfiter="between 1000 and 2000" },
                                 new tagcrowdfiltermodel(){ crowdfiter=" a<1000  || a>3000" },
                               }
                    }
                }
            };
        }

        public static int getcustomerid(string crowdfilter)
        {
            return bitconverter.toint32(guid.newguid().tobytearray(), 0);
        }

2. 实现需求

需求也很简单,就是依次获取 tagcrowdfiltermodel 中的 crowdfiter 字段再调用getcustomerid方法把人数赋值给totalcustomercount即可,这么简单的需求,如果让你来搞定,你该怎么实现这个逻辑? 没错,很多人可能就是两个foreach搞定。

        foreach (var year in estimatemodel.yearcrowdfilterdict.keys)
        {
            var yearcrowdfitlerlist = estimatemodel.yearcrowdfilterdict[year];

            foreach (var crowdfitler in yearcrowdfitlerlist)
            {
                crowdfitler.totalcustomercount = getcustomerid(crowdfitler.crowdfiter);
            }
        }

看似代码也很清爽,但现实哪有这么好的事情,真实情况是年份上可能还要套上一个客户类型,客户类型之上再套一个商品,商品之上再套一个商家,这样很深的层级你就需要多达3个foreach,4个foreach甚至5个foreach才能搞定,再放张图给大家看看,是不是看着头大...

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网