Search code examples
linq-to-entitiesnavigation-properties

Where clause on childs navigation properties


i tried to query a table whith a "where clause" base on multiple level child navigation properties.

I have :

  • table A whith navigation properties to :
    • Table B whith navigation properties to :
      • Table C

so i try :

var query = context.TableA.where(a => a.TableB_navProperties.where(b => b.TbableC_navProperties.where(c => c.prop=="testCondition")));

But this query is not correct: visual studio said : "impossible to convert system.collection.generic.IEnumerable< TableC > to bool"

How can i query table with condition on child of childs navigation properties ?

Thank's


Solution

  • I have found the solution. For the navigation properties wich are collection, use Any() instead of where () :

    var query = context.TableA.where(a => a.TableB_navProperties.Any(b => b.TbableC_navProperties.Any(c => c.prop=="testCondition"))); 
    

    This solution works but the sql generated use a lot of "Exist subquery" so i think it's not very efficient.