C# 左外连接 List allServicePacks=newlist(); 使用(var db=new DataContext()) { 所有服务包=( 来自db.ServicePack中的sp 在sp.State\u id上的db.States.DefaultIfEmpty()中加入st等于st.State\u id sp.ServiceType_Id上的db.ServiceTypes中的联接类型等于type.ServiceType_Id 哪里 (type.ServiceType_desc.ToLower()=“已接受”) orderby sp.AustState\u id 选择sp.ToServicePacksDTO(db)).ToList(); }
当前代码工作正常,直到我尝试在状态上执行外部联接。有没有其他方法可以轻松做到这一点?首先,您需要提供更多的细节,“工作正常,直到我尝试做xx” 什么不起作用?有错误吗?意外的结果 忘记DTO投影和C# 左外连接 List allServicePacks=newlist(); 使用(var db=new DataContext()) { 所有服务包=( 来自db.ServicePack中的sp 在sp.State\u id上的db.States.DefaultIfEmpty()中加入st等于st.State\u id sp.ServiceType_Id上的db.ServiceTypes中的联接类型等于type.ServiceType_Id 哪里 (type.ServiceType_desc.ToLower()=“已接受”) orderby sp.AustState\u id 选择sp.ToServicePacksDTO(db)).ToList(); },c#,linq-to-sql,outer-join,C#,Linq To Sql,Outer Join,当前代码工作正常,直到我尝试在状态上执行外部联接。有没有其他方法可以轻松做到这一点?首先,您需要提供更多的细节,“工作正常,直到我尝试做xx” 什么不起作用?有错误吗?意外的结果 忘记DTO投影和db.ServiceTypesjoin,要在db.ServicePacks和db.States之间执行LOJ,请执行以下操作: List<ServicePacksDTO> allServicePacks = new List<ServicePacksDTO>();
db.ServiceTypes
join,要在db.ServicePacks
和db.States
之间执行LOJ,请执行以下操作:
List<ServicePacksDTO> allServicePacks = new List<ServicePacksDTO>();
using (var db = new DataContext())
{
allServicePacks=(
from sp in db.ServicePacks
join st in db.States.DefaultIfEmpty() on sp.State_id equals st.State_Id
join type in db.ServiceTypes on sp.ServiceType_Id equals type.ServiceType_Id
where
(type.ServiceType_desc.ToLower() == "accepted")
orderby sp.AustState_id
select sp.ToServicePacksDTO(db)).ToList();
}
首先尝试一下,确保它有效(应该),然后添加其他连接和投影
var x = (from sp in db.ServicePacks
join st in db.States on sp.State_id equals st.State_id into spst
from x in spst.DefaultIfEmpty()
select new { /* fields */ }
).ToList();