欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net Lambda表达式与Linq (LINQ TO object)

程序员文章站 2022-04-03 23:42:17
Lambda表达式,是用来写匿名方法的。 在委托用得比较多,因为委托是传递方法的。 定义几个委托: public delegate void DoNoThing();//无参无返回值 public delegate void DoNoThingWithPara(sting name,int age) ......
lambda表达式,是用来写匿名方法的。
在委托用得比较多,因为委托是传递方法的。
 
定义几个委托:
public delegate void donothing();//无参无返回值
 
public delegate void donothingwithpara(sting name,int age);//有参无返回值
 
public delegate sting donothingwithreturn();//无参有返回值
 
public delegate int donothingwithparaandreturn(stiing name,int age);//有参有返回值
 
 
实例化委托
 
donothing dnt = ()=>{}; //无参无返回值方法
 
donothingwithpara dtwp = (x,y)=>{};//有参无返回值
 
donothingwithreturn dtwr = ()=>"hello"; //无参有返回值
 
donothingwithparaandreturn dntwpr = (x,y)=> 123; //有参有返回值
 
 
这就是lambda表达式的写法,本质就是方法。
 

linq to object
准备一个类:学生类student
 public class student
    {
        public int id { get; set; }

        public string name { get; set; }

        public int age { get; set; }

        public int classid { get; set; }
    }

准备学生数据:

list<student> stus= new list<student> {
            new student(){id=1,name="张三1",age=27,classid=1 },
            new student(){id=2,name="张三2",age=27,classid=1 },
            new student(){id=3,name="张三3",age=27,classid=1 },
            new student(){id=4,name="张三4",age=27,classid=1 },
            new student(){id=5,name="张三5",age=27,classid=1 },
            new student(){id=6,name="张三6",age=27,classid=2},
            new student(){id=7,name="张三7",age=19,classid=2},
            new student(){id=8,name="张三8",age=19,classid=2},
            new student(){id=9,name="张三9",age=19,classid=2},
            new student(){id=10,name="李四",age=32,classid=2 },
            new student(){id=11,name="李四1",age=32,classid=2 },
            new student(){id=12,name="李四2",age=32,classid=3 },
            new student(){id=13,name="李四3",age=32,classid=3 },
            new student(){id=14,name="李四4",age=32,classid=3 },
            new student(){id=15,name="李四5",age=32,classid=3 },
            new student(){id=16,name="李四6",age=32,classid=3 },
            new student(){id=17,name="李四7",age=37,classid=3 },
            new student(){id=18,name="李四8",age=37,classid=4 },
            new student(){id=19,name="王五",age=37,classid=4 },
            new student(){id=20,name="王五1",age=37,classid=4 },
            new student(){id=21,name="王五2",age=37,classid=4 },
            new student(){id=22,name="王五3",age=37,classid=4 },
            new student(){id=23,name="王五4",age=37,classid=4 },
            new student(){id=24,name="王五5",age=37,classid=4 },
            new student(){id=25,name="王五6",age=37,classid=4 },
            new student(){id=26,name="王五7",age=37,classid=4 }
            
        };
            查询班级id是1的
            var list = from s in stus
                       where s.classid == 1
                       select new
                       {
                           name = s.name,
                           classid = s.classid
                       };


            foreach (var item in list)
            {
                console.writeline(item.name + "---" + item.classid);
            }

//或者用框架的方法,查询年龄大于30的学生
            console.writeline("**********************");
            var list1 = stus.where(s => s.age > 30).select(s => new { id = s.id, name = s.name, age = s.age });
            foreach (var item in list1)
            {
                console.writeline(item.id + "---" + item.name + "--" + item.age);
            }

下面写一个分页的linq

var list = stus.where(s => s.age > 30)//条件筛选
                .select(s => new //投影
                {
                    name = s.name,
                    age = s.age,
                    classid = s.classid
                }).orderby(s => s.age)//排序
                .skip(2)//跳过几条
                .take(3);//获取几条 ,用于分页

            foreach (var item in list)
            {
                console.writeline(item.name + "--" + item.age);
            }

内连接,准备另外一个班级类

public class classinfo
    {
        public int id { get; set; }

        public string classname { get; set; }
    }


list<classinfo> classes = new list<classinfo>()
            {
                new classinfo(){id=1,classname="初级班" },
                new classinfo(){id=2,classname="中级班" },
                new classinfo(){id=3,classname="高级班" },
              //  new classinfo(){id=4,classname="超级班" },
            };

 

var list = from s in stus
                       join cla in classes
                       on s.classid equals cla.id
                      
                       select new
                       {
                           name = s.name,
                           classname = cla.classname,
                           age = s.age
                       };

            foreach (var item in list)
            {
                console.writeline(item.name+"========"+item.classname+"==="+item.age);
            }

左外连接

            var list1 = from s in stus
                        join c in classes
                        on s.classid equals c.id
                        into slist
                        from sc in slist.defaultifempty()
                        select new
                        {
                            name = s.name,
                            classname = sc== null ? "没有班级":sc.classname,
                            age = s.age
                        };
            foreach (var item in list1)
            {
                console.writeline(item.name + "========" + item.classname + "===" + item.age);
            }