MVC ---- Linq查询

时间:2022-06-15 14:58:52

Linq查询:编译后,会生成对应的标准查询运算符!所以说,Linq只是类似与Sql的一种更加友好的语法而已;

public class LinqDemo{

    public static void TestTen(){
List<User> usList =Five.GetUserList();
List<Dept> dtLsit =Five.GetDeptList(); //1. 从老集合中查询每一个元素存入新集合
//取数据到新的集合中
var newUserList = from u in usList select u; //2. Where条件 &&表示and
var newDtList = from d in dtLsit where d.DeptId= '' && d.PepName== select d; //3. Orderby
var newDtList = from d in dtLsit where d.DeptId= '' && d.PepName== orderby d.DeptId orderby d.PepNum descending select d; //4. Join关联数据
var joinResult = from u in usList join d in dtLsit on u.DeptId equals d.DeptId select new {UserName=u.Name,LoginName=u.LoginName,Deptname=d.DeptName};
//遍历
foreach(var item in joinResult){
Console.WriteLine(item.DeptName);
} //5. Group By 分组查询
var groupList = from u in usList group u by u.DeptId;
//遍历
foreach(var group in groupList){
Console.WriteLine(group.Key);
foreach(var item in group){
Console.WriteLine(item.Tostring());
}
}
}
}