I have several tables that are properly linked and mapped through FKs / link tables.
ie: link_table -> 1 to many relationship to table_one and table_two -> many to many relationship to table_three
var results = db.link_table.Where(l => l.table_one.RandomProperty == "value");
The above obviously works but I want to be able to make a select like this:
var results = db.link_table.Where(l => l.table_one.RandomProperty == "value" && l.table_two.table_three.RandomProperty == "anothervalue");
This fails because it seems not possible to access the properties that belong to table_three. The SQL query I would like should resemble something like this (the WHERE part is the most important):
SELECT * FROM link_table
LEFT JOIN table_one ON table_one.assoc = link_table.assoc
LEFT JOIN table_two ON table_two.assoc = link_table.assoc
LEFT JOIN table_three ON table_two.assoc = table_three.assoc
WHERE table_one.RandomProperty = "value" AND table_three.RandomProperty = "AnotherValue"
How would I be able to access the “RandomProperty” of table_three through the relational mapping of Linq2Entities? Is it even possible in one single line of code and thus getting the result I want in one automatically generated SQL query?
The in-depth answer is given to this question: linq to entities, a where in where clause? (inner where). It involves using the
.Any()command to search through the related many-to-many table data.i.e.: