C#:Linq 与 Lambda 的结合使用

0.

扩展方法使用: https://blog.csdn.net/qq_38261174/article/details/86347625

Lambda使用: https://blog.csdn.net/qq_38261174/article/details/86469552#comments

 

其实Linq就是扩展方法,源码如下:

namespace System.Linq {
       public static class Enumerable {
        //这里面有超级多的扩展方法
       }
}

1.

结合代码,了解如何使用 Linq。

定义一个对象 Die.cs 有3个属性,如下

            Die d1 = new Die {
                x = 2,
                y = 4,
                name = "d1"
            };
            Die d2 = new Die {
                x = 8,
                y = 5,
                name = "d2"
            };
            Die d3 = new Die {
                x = 5,
                y = 1,
                name = "d2"
            };
            Die d4 = new Die {
                x = 4,
                y = 3,
                name = "d4"
            };

            List ns = new List< Die>()
            {
               d1,
               d2,
               d3,
               d4
            };

好戏开场了》

            //public static decimal Sum(this IEnumerable source, Func selector);
            //对ns数组的元素的 x值进行求和
            int a1 = ns.Sum(delegate(Die die) {
                return die.x;
            });
            //下面的等价
            int a2 = ns.Sum( (Die die) => die.x);
            int a3 = ns.Sum( die => die.x);
            
            //对 x大于4的进行求和
            int a4 = ns.Where(die => die.x > 4).Sum(die => die.x);
            //另一种写法
            int a5 = (from die in ns where die.x > 4 select die.x).Sum();
            
            Console.WriteLine(a1 + " " + a2 + " " + a3 +" "+a4+" "+a5); //19  19  19  13  13

            //最大值,最小值
            int miny = ns.Min(die => die.y);
            int maxy = ns.Max(die => die.y);
            Console.WriteLine(miny +" "+maxy); //1  5

            //foreach
            ns.ForEach(die => {
                Console.WriteLine(die.name);
            });

            //条件筛选
            // name = d4 的 Die 只有一个
            var d_4 = ns.Where(die => die.name.Equals("d4")).Single();
            // name = d2 的 Die 多个
            var d_2 = ns.Where(die => die.name.Equals("d2")).ToList();


            //升序排序
            //要重新用一个数组存放,原数组顺序不变,新数组升序
            var sx = ns.OrderBy(die => die.x).ToList();
            sx.ForEach(die => {
                Console.Write(die.x); //2458
            });
            //降序
            //要重新用一个数组存放,原数组顺序不变,新数组降序
            var jx = ns.OrderByDescending(die => die.x).ToList();
            jx.ForEach(die => {
                Console.Write(die.x); //8542
            });
            //sort排序,自定义原数组的顺序改变 
            //总体上y从小到大排,y一样则x从小到大排
            ns.Add(new Die { x=1,y=3});  //有两个 die.y = 3;
            ns.Sort((die1, die2) => {
                if (die1.y == die2.y) {
                    return die1.x.CompareTo(die2.x);
                }
                else {
                    return die1.y.CompareTo(die2.y);
                }
            });
            Console.WriteLine("");
            ns.ForEach(die => {
                Console.WriteLine(die.y +"  "+die.x);
                //1  5
                //3  1    //y一样则按x从小到大
                //3  4
                //4  2
                //5  8

            });

            //分组
            //public static IEnumerable> GroupBy(this IEnumerable source, Func keySelector);
            //按y一样的分在一组
            var group = ns.GroupBy(die => die.y);
            foreach (var gp in group) {
                // gp.Key; //组名(这里是y)
                //每一组里面还有(单个或多个)Die
                foreach (var d in gp) {
                    int dx = d.x;
                    //
                }
            }

 

完!

 

 

 

 

 

你可能感兴趣的:(C#:Linq 与 Lambda 的结合使用)