.net Lambda表達式與Linq (LINQ TO object)


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);
            }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM