LINQ在实际中的应用

            List stus = new List()
            {
                new Stu(){id = 1, name ="a"},
                new Stu(){id = 2, name ="b"},
                new Stu(){id = 3, name ="c"},
                new Stu(){id = 4, name ="d"},
                new Stu(){id = 1, name ="e"},
            };

            //找出所有id为1的
            var newStu = stus.Where(m => m.id == 1);


            ///按照id大小进行排序
            //方法一:使用OrderBy、OrderByDescending扩展方法
            var newStu1 = stus.OrderBy( s=>s.id ).toList(); //倒叙使用OrderByDescending

            //方法二:使用Queryable的ThenBy、ThenByDescending进行多级排序
            var result = stus.OrderBy(x => x.Prop1)
                .ThenBy(x => x.Prop2);

            方法三:LINQ中的orderby字句排序
            var res = from x in stus
                    orderby x.id, x.name
                    select x;

            
//找出数组相同数字和大于11的
var strA = new[] {3, 4, 5, 6};
var strB = new[] {7, 8, 9, 10};

var strAB = from a in strA
            from b in strB
            let sum = a + b
            where sum > 11
            select new { a, b, sum};

Linq中也可以使用sql中的join on

你可能感兴趣的:(C#特性,linq,windows,c#)