C# 如果表中存在行,如何合并
我在下面的声明中加入了3个表:C# 如果表中存在行,如何合并,c#,linq,entity,C#,Linq,Entity,我在下面的声明中加入了3个表: var data = from x in dbContext.Base_Agencies from u in dbContext.Base_AgencyInstances from o in dbContext.Payment2Account_SecurityRuleAgencies where u.AgencyId == x.AgencyId
var data = from x in dbContext.Base_Agencies
from u in dbContext.Base_AgencyInstances
from o in dbContext.Payment2Account_SecurityRuleAgencies
where u.AgencyId == x.AgencyId
where o.AgencyId == x.AgencyId
where u.AgencyInstanceId == param.AgencyInstanceId
select new RsSecurityParamsResult
{
AgencyId = x.AgencyId,
AgencyNameView = u.AgencyNameView,
Stamp = u.Stamp,
Pni = x.Pni,
Prefix = u.Prefix,
ServiceEnabled = o.ServiceEnabled,
DisabledDateTime = o.DisabledDateTime,
AmountHourTresholdWarning = o.AmountHourTresholdWarning,
AmountHourTresholdStop = o.AmountHourTresholdStop,
CountHourTresholdWarning = o.CountHourTresholdWarning,
CountHourTresholdStop = o.CountHourTresholdStop
};
问题是,在某些示例中,表“o”中没有代理行。在这种情况下,我只想从其他表中选择值,除了“o”表。我该怎么做呢?我想你可能想做一个
左外连接
var data = from x in dbContext.Base_Agencies
join u in dbContext.Base_AgencyInstances
on u.AgencyId == x.AgencyId
join o in dbContext.Payment2Account_SecurityRuleAgencies
on o.AgencyId == x.AgencyId into lrs
from lr in lrs.DefaultIfEmpty()
select new RsSecurityParamsResult
{
AgencyId = x.AgencyId,
AgencyNameView = u.AgencyNameView,
Stamp = u.Stamp,
Pni = x.Pni,
Prefix = u.Prefix,
ServiceEnabled = lr.ServiceEnabled ?? "Default",
...... // other code similar ........
};
正确的解决方案是:
var data = from x in dbContext.Base_Agencies
join u in dbContext.Base_AgencyInstances
on x.AgencyId equals u.AgencyId
join o in dbContext.Payment2Account_SecurityRuleAgencies
on x.AgencyId equals o.AgencyId into lrs
from lr in lrs.DefaultIfEmpty()
where u.AgencyInstanceId == param.AgencyInstanceId
select new RsSecurityParamsResult
{
AgencyId = x.AgencyId,
AgencyNameView = u.AgencyNameView,
Stamp = u.Stamp,
Pni = x.Pni,
Prefix = u.Prefix,
ServiceEnabled = lr.ServiceEnabled,
DisabledDateTime = lr.DisabledDateTime,
AmountHourTresholdWarning = lr.AmountHourTresholdWarning ,
AmountHourTresholdStop = lr.AmountHourTresholdStop,
CountHourTresholdWarning = lr.CountHourTresholdWarning ,
CountHourTresholdStop = lr.CountHourTresholdStop
};
您所说的表“o”中没有代理行是什么意思?情况:1。在表“x”中,有一行包含一些agencyId,2。在表“u”中,有一行具有相同的agencyId 3。在表“o”中,没有一行具有相同的agencyIdyeah,您可能需要检查后面的答案。您的答案中有几个语法错误,但主要思想是正确的,这对我帮助很大。