Linq查询涉及4个联接表,到一个多对多表

本文关键字:一个 查询 4个 Linq | 更新日期: 2023-09-27 18:21:21

当任何记录具有赔偿类型的保证金或放弃保证金时,我需要返回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;

我越来越近了。我的验证现在在"履约保函"或"付款保函"的情况下正常工作,但在BondRequirementWaved的情况下不起作用。这在EF中是一个bool,在SQL server中也是一个bit。在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;

Linq查询涉及4个联接表,到一个多对多表

第二个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"));

很难看出你到底想要什么,但我认为这正是基于你的问题,在没有明确实体模型的情况下你想要的。