Linq查询涉及4个连接表,到多个表到多个表

时间:2015-11-10 21:32:03

标签: c# entity-framework linq

当任何记录具有赔偿类型的债券或放弃债券时,我需要返回true。我认为,由于内部连接的发生方式,这是行不通的。

var HasBondorWaived = (from a in context.Allocations
                       join p in context.Permits on a.PermitGUID equals p.GUID
                       join i in context.Indemnities on a.IndemnityGUID equals i.GUID
                       join t in context.IndemnityTypes on a.IndemnityAreaTypeGUID equals t.GUID
                       where (p.GUID.Equals(PermitGuid)
                       && (t.Description.Equals("Performance Bonds") || t.Description.Equals("Payment Bonds")))
                       || p.BondRequirementWaived where p.GUID.Equals(PermitGuid)
                       select a).Any();
return HasBondorWaived;

我越来越近了。我的验证现在在“Performance Bond”或“Payment Bond”的情况下正常工作,但在BondRequirementWaved的情况下不起作用。这是EF中的bool,有点在SQL服务器中。在BondRequirementWaved的情况下,它返回false。

using (var context = new KEPTEntities())
        {
            var HasBondorWaived = (from a in context.Allocations
                                   join p in context.Permits on a.PermitGUID equals p.GUID
                                   join i in context.Indemnities on a.IndemnityGUID equals i.GUID
                                   join t in context.IndemnityTypes on i.IndemnityTypeGUID equals t.GUID
                                   where (p.GUID.Equals(PermitGuid) 
                                   && (t.Description.Equals("Performance Bonds") 
                                   || t.Description.Equals("Payment Bonds") 
                                   || p.BondRequirementWaived))
                                   select a).Any();
            return HasBondorWaived;

1 个答案:

答案 0 :(得分:2)

第二个where子句不会像你期望的那样工作。你需要删除它。

你可能想要这个:

where (p.GUID.Equals(PermitGuid)
&& (t.Description.Equals("Performance Bonds") || t.Description.Equals("Payment Bonds")
|| p.BondRequirementWaived))

假设您已设置导航属性,则更清晰:

var HasBondorWaived=context.Allocations
  .Where(a=>a.Permits.GUID.Equals(PermitGuid))
  .Any(a=>a.Permits.BondRequirementWaived || 
    a.Indemnities.Any(i=>i.IdemnityType.Description=="Performance Bonds" || i.IdemnityType.Description=="Payment Bonds"));

很难看到你真正要求的东西,但我认为这是你想要的,基于你的问题,没有明确的实体模型。