Linq query involving 4 joined tables, to a many to many table

Chris

I need to return true when any record either has an Indemnity type of bond, or has the bond waived. I think because of the way inner joins are happening this is not working.

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;

I'm getting closer. My validation is now working correctly in the case of "Performance Bond" or "Payment Bond", but is not working in the case of BondRequirementWaved. This is a bool in the EF, and a bit in SQL server. In the case of BondRequirementWaved, it is returning 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;
Robert McKee

The second where clause won't work as you expect. You need to remove it.

You probably want this:

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

Assuming you have navigation properties set up, this is much cleaner:

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"));

Kind of hard to see what you actually asking for, but I think that is what you want based on your question and without a clear entity model.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Linq Many to Many query

Join two tables, in which second table needs to be joined many times

Counting relationships in many to many table between joined tables in Sequelize.js

Write a linq query with many to many scenario and other table

how to create a left join query for a many to many related table with LINQ

Correct Postgresql SQL for query involving COUNT and joined tables

SQL query Pivot Many to Many to Many Table

select the relationship on one to many in two table joined

Laravel - query to join tables in Many to many relation

MySQL Query tables in many to many relationship

PostgreSQL update a table from another one joined as many to many

Entity Framework 6.1.3; code first migrations many to many joined table

How to select needed records in joining table using LINQ with tables which have many to many relationship?

Table with many columns or many small tables?

many to many linq query with like expression

SQL/ Linq query select from many to many

Linq Query relating Many to Many relationship

Linq lambda expression many to many table select

Use linq to count in many to many table

one query for many similar tables

Multiple query with many tables in MySql

One table or many tables with relationships

How to get name of product from many to many even if it is not in the related table SQL or EF linq query

MYSQL Many to many query to same table

Query from a many to many relationship table

select query with many-on-many table

Multi table query with sums, One to Many to Many

SQLAlchemy - Query Many to Many with association table

How to write query on many to many association table