There are several levels of "laziness" involved here. One is the inherent laziness of IEnumerable
implementations, the other is the lazy implementation you add yourself to your property.
Your first implementation will hit the database once, the first time AllCustomers
is accessed. It will construct the query in GetAllCustomers
and perform it when you call ToList
, storing the results locally.
Your second implementation will also hit the database only once (assuming your LINQ implementation is halfway decent). However, this will be later than in the first scenario - even calling your AllCustomer
property will only return an IQueryable
, which will only be performed when AllCustomers
is actually accessed or enumerated. This might be immediately after, or not - it's a lazier implementation. Again, assuming your LINQ provider isn't too stupid, iterating over the entire collection will still only hit the DB once.
Why should you choose the first option anyway? Because (again, depending on the implementation), iterating over AllCustomers
twice might hit the DB twice. Resharper, conveniently enough, warns us when we have a possible multiple enumeration of an IEnumerable
. Storing it in a local List
will ensure we keep a cached local copy. To make sure this is expressed explicitly in the code, consider exposing an IReadOnlyList
instead of an IEnumerable
.