
正文
Linq的常见查询
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
首先定义几个模型类:
/// <summary>
/// 员工类
/// </summary>
public class Employee
{
/// <summary>
/// 员工id
/// </summary>
public int Empid { get; set; }
/// <summary>
/// 部门id
/// </summary>
public int DeptId { get; set; }
/// <summary>
/// 员工姓名
/// </summary>
public string EmpName { get; set; }
/// <summary>
/// 员工编号
/// </summary>
public string EmpCode { get; set; }
}
/// <summary>
/// 部门模型
/// </summary>
public class DeptModel
{
/// <summary>
/// 部门id
/// </summary>
public int DeptId { get; set; }
/// <summary>
/// 部门名称
/// </summary>
public string DeptName { get; set; }
}
/// <summary>
/// 员工类
/// </summary>
public class Employee
{
/// <summary>
/// 员工id
/// </summary>
public int Empid { get; set; }
/// <summary>
/// 部门id
/// </summary>
public int DeptId { get; set; }
/// <summary>
/// 员工姓名
/// </summary>
public string EmpName { get; set; }
/// <summary>
/// 员工编号
/// </summary>
public string EmpCode { get; set; }
}
/// <summary>
/// 部门模型
/// </summary>
public class DeptModel
{
/// <summary>
/// 部门id
/// </summary>
public int DeptId { get; set; }
/// <summary>
/// 部门名称
/// </summary>
public string DeptName { get; set; }
}
组装数据:
/// <summary>
/// 员工数据
/// </summary>
private static List<Employee> Emp = new List<Employee>()
{
new Employee {Empid=,DeptId=,EmpName="王芳",EmpCode="" },
new Employee {Empid=,DeptId=,EmpName="韩丽",EmpCode="" },
new Employee {Empid=,DeptId=,EmpName="李飞",EmpCode="" },
new Employee {Empid=,DeptId=,EmpName="李丽",EmpCode="" },
new Employee {Empid=,DeptId=,EmpName="王二麻",EmpCode="" },
new Employee {Empid=,DeptId=,EmpName="刘慧",EmpCode="" },
new Employee {Empid=,DeptId=,EmpName="张飞",EmpCode="" },
new Employee {Empid=,DeptId=,EmpName="测试人员",EmpCode="" },
};
/// <summary>
/// 部门数据
/// </summary>
private static List<DeptModel> Dept = new List<DeptModel>()
{
new DeptModel {DeptId=,DeptName="产品部" },
new DeptModel {DeptId=,DeptName="管理层" },
new DeptModel {DeptId=,DeptName="人事部" },
new DeptModel {DeptId=,DeptName="研发部" },
new DeptModel {DeptId=,DeptName="项目部" },
new DeptModel {DeptId=,DeptName="市场部" },
new DeptModel {DeptId=,DeptName="测试部门" },
};
1、内连接(join)查询
//Lambda写法
var data = Emp.Join(Dept, e => e.DeptId, d => d.DeptId, (e, d) => new { e, d }).ToList();
//Linq写法
var data2 = (from e in Emp
join d in Dept
on e.DeptId equals d.DeptId
select new { e, d }).ToList();
2、左(left join )连接查询
//Linq写法
var data3 = (from e in Emp
join d in Dept
on e.DeptId equals d.DeptId into list
from dept in list
select new { e, dept }).ToList();
//Lambda写法
var data4 = Emp.GroupJoin(Dept, e => e.DeptId, d => d.DeptId, (e, d) => new { e, d = d.FirstOrDefault() }).ToList();
//Linq写法
var data3 = (from e in Emp
join d in Dept
on e.DeptId equals d.DeptId into list
from dept in list
select new { e, dept }).ToList();
//Lambda写法
var data4 = Emp.GroupJoin(Dept, e => e.DeptId, d => d.DeptId, (e, d) => new { e, d = d.FirstOrDefault() }).ToList();
3、交叉(Corss join)连接
//Linq写法
var data5 = (from e in Emp
from d in Dept
select new { e, d }).ToList();
//Lambda写法
var data6 = Emp.SelectMany(emp => Dept.Select(dept => new { emp, dept })).ToList();
Lambda
Func<int, string> fun = (int a) => { return "返回值" + a; };
Func<int, string> fun2 = delegate (int a) { return "返回值" + a; };
Action action = () => { Console.WriteLine("返回值1"); };
Action action2 = delegate () { Console.WriteLine("返回值2"); };
Predicate<int> predicate = delegate (int a) { return a > ; };
Predicate<int> predicate3 = (int a) => { return a > ; };






