-1

These two Entity Framework Core queries in .Net Core brings the same results.

 1) var _folders = _box.Folders.Where(b => b.Id == box.Id).SingleOrDefault();

 2) var _folders = _box.Folders.SingleOrDefault(b => b.Id == box.Id);

The first one uses SingleOrDefault at the end, with Where after the child object, and the second one doesn't use Where. What's the difference between both? There is a benefit of using one over the other?

marcaldo
  • 59
  • 4
  • They are the same thing, they both take an expression to filter – johnny 5 Oct 11 '18 at 14:45
  • Related, maybe duplicate:https://stackoverflow.com/questions/1024559/when-to-use-first-and-when-to-use-firstordefault-with-linq and https://stackoverflow.com/questions/10110013/order-of-linq-extension-methods-does-not-affect-performance – Tim Schmelter Oct 11 '18 at 14:47
  • _"There is a benefit of using one over the other?"_ Sure, readability, but this is opinion based – Tim Schmelter Oct 11 '18 at 14:51

2 Answers2

1

It all comes down to implementation. Some ORMs will handle those two queries slightly differently. But as you've already noticed, EF Core treats them the same way (as it should, because they are theoretically the same).

Joe Phillips
  • 49,743
  • 32
  • 103
  • 159
0

These should equate to the same query produced by EF. The SingleOrDefault version is just a shortcut so you don't have to include the Where. You can use SQL Profiler (if using SQL Server) to test the output to verify the queries truly are the same.

user1011627
  • 1,741
  • 1
  • 17
  • 25