如果表中的行存在,如何合并

时间:2017-06-21 11:24:58

标签: 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
                   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'表。我该怎么做?

2 个答案:

答案 0 :(得分:0)

我想你可能想要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 ........
                   };

答案 1 :(得分:0)

正确的解决方案是:

  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
                   };