Calling database upon ToList() method call

817 Views Asked by At

From my repository I return different List using ToList() method.

The problem is when I run some more LINQ on this returned result (i.e. of type List) it generates a database call for this too. Apparently this second linq calll is pure LINQ to Entity and database should not be called. See below what I am doing.

List<User> us = userRepository.GetMany(u => filterStatusIds.Contains(u.UserStatus.Id));
if (!string.IsNullOrEmpty(name))
us = (from u in us
      where u.DisplayName.Contains(name)
      select u).ToList(); // this ToList should not call database

Any help or idea to stop this additional database calls?

1

There are 1 best solutions below

5
On

Return IEnumerable<User> or IQueryable<T> from repository, it will not query the database.

Only second call will do this, after ToList().