·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 关系型数据之LinQ基本查询
如下图所示关系型数据,LinQ语句为:
var Query= from a in data.A where a.admin==传入的用户名&&a.pwd==传入的密码(可以传入一个model实体) join b in data.B on a.bid equals b.id into ab from r1 in ab.DefaultIfEmpty() join c in data.C on r1.cid equals c.id into bc from r2 in bc.DefaultIfEmpty() join d in data.D on r2.did equals d.id into cd from r3 in cd.DefaultIfEmpty()
select new { //这里是table A 的id; Id = a.Id, //这里是table B的Name; Name = r1.Name, PRo = r2.Pro, ipAdd = r3.IpAdd }; //转成数组形式; var list = Query.ToList(); //list[0].Name即表示整个LinQ查询的结果的第一条数据的table B的Name,以此类推;
注: r1可以.出table B的所有数据...以此类推;
r2==table C;
r3==table D;
=====================================================================================================
1 1、左连接: 2 3 var LeftJoin = from emp in ListOfEmployees 4 join dept in ListOfDepartment 5 on emp.DeptID equals dept.ID into JoinedEmpDept 6 from dept in JoinedEmpDept.DefaultIfEmpty() 7 select new 8 { 9 EmployeeName = emp.Name, 10 DepartmentName = dept != null ? dept.Name : null 11 }; 12 13 14 15 2、右连接: 16 17 var RightJoin = from dept in ListOfDepartment 18 join employee in ListOfEmployees 19 on dept.ID equals employee.DeptID into joinDeptEmp 20 from employee in joinDeptEmp.DefaultIfEmpty() 21 select new 22 { 23 EmployeeName = employee != null ? employee.Name : null, 24 DepartmentName = dept.Name 25 }; 26 27 28 29 3、内连接: 30 31 var query = from t in entitiy.TB_GCGL_ADA_USER 32 join p in entitiy.TB_GCGL_ZY_ZYK 33 on t.ETPRS_CODE equals p.ETPRS_CODE 34 35 select new TB_USER_ZYK 36 { 37 USER_ID = t.USER_ID, 38 USER_NAME = t.USER_NAME, 39 USER_PASSWord = t.USER_PASSWORD, 40 41 };