I’ve taken control of some entity framework code and am looking to refactor it. Before I do, I’d like to check my thoughts are correct and I’m not missing the entity-framework way of doing things.
Example 1 – Subquery vs Join
Here we have a one-to-many between As and Bs. Apart from the code below being hard to read, is it also inefficient?
from a in dataContext.As
where ((from b in dataContext.Bs
where b.Text.StartsWith(searchText)
select b.AId).Distinct()).Contains(a.Id)
select a
Would it be better, for example, to use the join and do something like this?
from a in dataContext.As
where a.Bs.Any(b => b.Text.StartsWith(searchText))
select a
Example 2 – Explicit Joins vs Navigation
Here we have a one-to-many between As and Bs and a one-to-many between Bs and Cs.
from a in dataContext.As
join b in dataContext.Bs on b.AId equals a.Id
join c in dataContext.Cs on c.BId equals b.Id
where c.SomeValue equals searchValue
select a
Is there a good reason to use explicit joins rather than navigating through the data model? For example:
from a in dataContext.As
where a.Bs.Any(b => b.Cs.Any(c => c.SomeValue == searchValue)
select a
Sometimes it is necessary to use the join-style and subqueries to get control over certain aspects of a LINQ 2 SQL query. This is not the case here. The “navigation” style is strictly preferable. Sometimes, it even comes with performance benefits because LINQ to SQL used smarter SQL patterns.
I don’t just want to answer with “you’re right” so let me say that I have a lot of experience with LINQ to SQL. I’m using it on two bigger projects where performance is critical and almost every generated SQL statement is perf-tested by me. So this answer bears some authority and isn’t just a random internet opinion.