Linq 多表左外连接 Left Join查询

来自一搜便知
跳到导航 跳到搜索
var result = (await (from device in userDbContext.Devices
    join extion in userDbContext.ExtensionTables on device.Id equals extion.ObjectGid
    join custom in userDbContext.CustomFields on extion.CustomFieldId equals custom.Id
    join department in userDbContext.Departments on device.DepartmentId equals department.Id into departments
    from department1 in departments.DefaultIfEmpty()
    join operateUser in userDbContext.Users on device.OperateUserId equals operateUser.Id into operateUsers
    from operateUser1 in operateUsers.DefaultIfEmpty()
    join principalUser in userDbContext.Users on device.PrincipalUserId equals principalUser.Id into principalUsers
    from principalUser1 in principalUsers.DefaultIfEmpty()
    where device.IsDelete == false && (content == null ? true : (device.SerialNumber.Contains(content.Trim()) ||             
          device.Name.Contains(content.Trim())))&& custom.CategoryType == CategoryType.Device && custom.IsDelete == false
    select new
    {    
        BaseInfo = new BaseInfo(device, operateUser1, principalUser1, department1),
        CustomInfo = new CustomInfo(custom, extion)
    }).AsNoTracking().ToArrayAsync()).GroupBy(a => a.BaseInfo).Select(a => new DeviceInfoDto(a.Key, (from b in a select     
    b.CustomInfo).ToArray()));