Alex R.
Alex R.

Reputation: 861

Entity Framework - Use Join clause on an entity collection

I'm a beginner beginner with entity framework and I need your help to build a query.

I have the following tables :

table1

idTable1  myDataTable1

table2

idTable2 myDataTable2 #idTable1 #idTable3

table3

idTable3 myDataTable3

I need to have an equivalent of the following sql request :

SELECT * FROM table1 t1
    INNER JOIN table2 t2
    WHERE t1.idTable1 = t2.idTable1 
        and t2.idTable3 = 4;

I try to do this with entityframework like that but it doesn't work :

List<table1> l = context.table1.Where(tab => (tab.table2.idTable3 == 4)).ToList<table1>();

The problem is that "table2" is an EntityCollection, I need to choose one with "ElementAt" to call "idTable3", but I want to search in all table2 elements...

Have you any idea ?

Regards

Edit : At the moment, I use the following Linq query but if you have some ideas...

from t1 in context.table1 from t2 in t1.table2 where t2.table3.idTable3 == 4 select t1

Upvotes: 0

Views: 1163

Answers (1)

Rob Epstein
Rob Epstein

Reputation: 1500

var result = context.table1.Where(x => x.table2.Any(y => y.idTable3 == 4)).ToList();

Upvotes: 1

Related Questions