[Solved] Seeking feedback on my design of LINQ expressions [closed]


You’re comparing apples and oranges. Each of the approaches you list has benefits and drawbacks, so it’s impossible to answer which is the “preferred way.”

Likewise, many of these examples will return immediately because they use deferred execution, so they’ll technically run faster, but won’t actually make your program run any faster.

But if you’re just trying to get an in-memory list of all the Employees, and you don’t care about change tracking, as you say, I’d prefer this:

public IReadOnlyCollection<Employee> EmployeeList
{
     get { return Context.Employees.AsNoTracking().ToList(); }
}
  • You don’t need a .Select(e => e): it doesn’t do anything.
  • AsNoTracking() will give you a minor performance improvement.
  • .ToList() ensures that the values are actually loaded into memory before the method returns.
  • I personally like using IReadOnlyCollection<>s when I want to indicate that I’m providing something that’s been loaded into memory, but I don’t want to give the impression that consuming code is expected to add/remove values from the collection that I return.

1

solved Seeking feedback on my design of LINQ expressions [closed]