linq表达式 多表关联查询,左连接查询

本文详细介绍了使用C#和LINQ进行多表关联查询的方法,包括如何连接批发商补货、店铺、员工及批发商等数据表,实现数据的高效整合与展示。通过具体代码示例,展示了如何处理可能存在的空值情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

多表关联查询
             from x in _wholesalerReplenishmentDetailRepository.GetAll()
                      join n in _wholesalerReplenishmentRepository.GetAll() on x.WholesalerReplenishmentId equals n.Id  //批发商补货
                      join y in _store.GetAll() on x.StoreId equals y.Id //店铺
                      join z in _wholesalerEmployeeRepository.GetAll() on x.EmployeeId equals z.Id into groupsz
                      from grpz in groupsz.DefaultIfEmpty() //业务员(可能为空)
                      join m in _wholesalerRepository.GetAll() on x.WholesalerId equals m.Id into groupsm
                      from grpm in groupsm.DefaultIfEmpty() //批发商(可能为空)
                      orderby x.CreationTime descending
                      select new WholesalerReplenishmentFullDto
                      {
                          Id = x.Id,
                          CompanyName = grpm.CompanyName,
                          EmployeeName = x.EmployeeId == null ? grpm.ContactPerson : grpz.EmployeeName,//可能批发商自己补货,//可能批发商自己补货,
                          StoreName = y.Name,
                          StoreBossName = y.BossName,
                          CreatTime = x.CreationTime,
                          CreationAddress = n.CreationAddress,
                          OpratorId = x.LastModifierUserId == null ? x.CreatorUserId : x.LastModifierUserId,
                          OpratorTime = x.LastModificationTime == null ? x.CreationTime : x.LastModificationTime,
                          ProductId = x.ProductId
                      };

let设置临时条件

          from u in users
             let number = Int32.Parse(u.Username.Substring(u.Username.Length - 1))
             where u.ID < 9 && number % 2 == 0
             select u

 

转载于:https://www.cnblogs.com/KQNLL/p/9871919.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值