专业编程基础技术教程

网站首页 > 基础教程 正文

使用Linq在MVC中实现做左连接,进行多表查询

ccvgpt 2024-07-23 01:35:42 基础教程 8 ℃

在软件开发中经常进行多表查询,通过关联字段查询到对应的信息,其中左连接数经常使用的。

var query = from o in db.User

使用Linq在MVC中实现做左连接,进行多表查询

join d in db.DepartMent

on o.departmentId equals d.departmentId into dc

from dci in dc.DefaultIfEmpty()

join e in db.Role

on o.roleId equals e.roleId into ec

from eci in ec.DefaultIfEmpty()

join dut in db.Duty

on o.userId equals dut.userId into fc

from bcc in fc.DefaultIfEmpty()

join cls in db.ClassesType

on bcc.classesId equals cls.classesId into gc

from clcc in gc.DefaultIfEmpty()

where o.userName.Contains(username)

select new UserView

{

userId = o.userId,

userName = o.userName,

passWord = o.passWord,

loginName = o.loginName,

DepartMentName = dci.departmentName,

RoleName = eci.roleName,

status = o.status,

creatTime = o.creatTime,

DutyName = clcc.classesName

};

Tags:

最近发表
标签列表