0

Is there a different between the two statements below, perhaps in terms of performance or readability, assuming you have a model named RateCountry with CountryCode as one of the properties. Of course in my project I will only have one return statement

public RateCountry GetRateCountry(string countryCode, int rateId)
{
    return _directConnectContext.RateCountries.FirstOrDefault(rc => rc.CountryCode == countryCode && rc.RateID == rateId);

    return _directConnectContext.RateCountries.Where(rc => rc.CountryCode == countryCode && rc.RateID == rateId).FirstOrDefault(); 
}
5
  • 1
    EF is an ORM. LINQ queries generate SQL queries in the end. Compare the generated SQL text. I suspect they'll be either identical or equivalent. There's only one way to write SELECT TOP 1 FROM X where ... after all Commented Mar 19, 2018 at 11:20
  • As long as your are dealing with an IQueryable which if _directConnectContext is an EF DB context you are, then there is no difference. Commented Mar 19, 2018 at 11:21
  • @AshleyMedway wrong source. That's for Enumerable, not Queryable Commented Mar 19, 2018 at 11:22
  • @AshleyMedway Queryable's source doesn't matter though, the generated SQL query does. That's done by the provider. It's easier to simply check the generated queries Commented Mar 19, 2018 at 11:25
  • 1
    First statement is simply a shortcut for (a concise version of ) the second, hence performance should be the same. What about readability, it's a matter of personal preference, hence is opinion based. Commented Mar 19, 2018 at 11:30

1 Answer 1

1

Equality of LINQ statements is defined by equality of generated SQL code. If you look at generated SQL code of both queries, you will see that they are the same. So, answering your question - no, there is no difference.

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

Comments

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.