0

I have been using Linq methods for a while in my project. But got to know an another way of writing them.

What I wrote:

dbContext.Currencies
         .FirstOrDefault(cr => cr.Name.EqualInvariant(currencyName))

Another way I got to know:

(from curr in dbContext.Currencies 
 where curr.Name == currencyName select curr).FirstOrDefault();

Which method is more efficient?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Please check this https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/query-syntax-and-method-syntax-in-linq – Karan Dec 30 '21 at 06:02
  • 2
    They do exactly the same, just alternative syntax. So.. equally efficient. – Johan Donne Dec 30 '21 at 06:02
  • No difference, the two line will generate the same query, it's a personal preference to use one of these styles ... personally I prefer the first one (LINQ) because it more compact and readable – Ibram Reda Dec 30 '21 at 06:29
  • If we are talking about LINQ to Objects, first one is more effective, because there is additional `Where` in second query. For LINQ to Entities (EF, Linq To Sql, NHibernate, linq2db, etc.) both queries are identical. – Svyatoslav Danyliv Dec 30 '21 at 06:40

0 Answers0