0

Lets say I have these three methods:

public Customer GetCustomerByCustomerGuid(Guid customerGuid)
{
    return GetCustomers().FirstOrDefault(c => c.CustomerGuid.Equals(customerGuid));
}

public Customer GetCustomerByEmailAddress(string emailAddress)
{
    return GetCustomers().FirstOrDefault(c => c.EmailAddress.Equals(emailAddress, StringComparison.OrdinalIgnoreCase));
}

public IEnumerable<Customer> GetCustomers()
{
    return from r in _customerRepository.Table select r;
}

//_customerRepository.Table is this:
public IQueryable<T> Table
{
    get { return Entities; }
}

Would this cause a query to the database each time I make a call to GetCustomerByEmailAddress() / GetCustomerByCustomerGuid() or would EF cache the results of GetCustomer() and query that for my information?

On the other hand, would it just cache the result of each call to GetCustomerByEmailAddress() / GetCustomerByCustomerGuid()

I am trying to establish the level of manual caching I should go to, I really dislike running more SQL queries than are absolutely necessary.

2 Answers 2

4

Yes, it'll query the database every time.

Your concern should be to optimize your code so that it only queries for and returns the record you need. Right now it's pulling back the entire table and then you're filtering it with FirstOrDefault().

Change public IEnumerable<Customer> GetCustomers() to public IQueryable<Customer> GetCustomers() to make it more efficient.

Sign up to request clarification or add additional context in comments.

16 Comments

That's not how LINQ to EF works. The LINQ Provider is smart enough to translate FirstOrDefault() into select top 1. Basically, any query methods used for a LINQ to EF query that have equivalent SQL functions will be executed as SQL in favor of operating on the result set in C#.
It would if it were returning an IQueryable rather than IEnumerable.
@evanmcdonnal - That was my understanding as well. The query is generated based on the full LINQ query.
@AnthonyChu - In my case _customerRepository.Table is IQueryable<> but the poster would not of known that.
@AnthonyChu how do you know the type returned by GetCustomers()?
|
1

It will result in a call to the database every time. I actually asked a similar question earlier today and you can see more here: Why does Entity Framework 6.x not cache results?

5 Comments

Ah I see. Out of interest how did you see if EF was hitting the DB or not?
I used SQL Server Profiler to trace all calls to my database.
I knew something like that existed but couldn't see it in SQL Express (which I use at home). I'll look a little deeper.
I guess that's not part of SQL Express. But it will definitely call the database every single time. I was dealing with this today unfortunately.
.Find() it is then. Thanks for the information, I'll mark this as the answer (edit: in 4 minutes :) ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.