如果 table 中的行存在,如何合并
How to merge if row in table exists
我在下面的语句中加入了 3 tables:
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
};
问题是在某些示例中 table 'o' 中不会有代理行。在这种情况下,我只想 select 来自其他 table 的值,'o' table 除外。我应该怎么做?
我想你可能想要LEFT OUTER JOIN
像
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
};
我在下面的语句中加入了 3 tables:
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
};
问题是在某些示例中 table 'o' 中不会有代理行。在这种情况下,我只想 select 来自其他 table 的值,'o' table 除外。我应该怎么做?
我想你可能想要LEFT OUTER JOIN
像
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
};