Accessing Joined Tables in Linq2SQL When Returning IQueryable
I have a linq query which draws a data set out of a database, but unfortunately due to the legacy nature of the database it requires a manual "join" to be enacted as part of the query.
Now I need to add further optional filters to the data set, some of which rely on the joined table - if I return the data set from the first query as a .AsQueryable() how do I go about accessing the joined table, as it doesn't seem to be available from the IQuerable?
Example:
var myQuery = (from o in db.FirstTable
join t in db.SecondTable on o.SecondID 开发者_开发百科equals t.ID
select o).AsQueryable();
// (optional - if statement excluded for brevity)
myQuery = from x in myQuery where t.SomeField.Equals("My Filter Value");
Whichever properties you need out of the table, you have to grab in the select. So, change your select from "select o" to "select o.Property1, o.Property2, t.Property1, t.Property2"
精彩评论