.net Lambda表达式与Linq (LINQ TO object)
Lambda表达式,是用来写匿名方法的。
在委托用得比较多,因为委托是传递方法的。
// An highlighted block
定义几个委托:
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);
}