Search code examples
entity-frameworkglimpse

Glimpse SQL shows select top 2 very often


See in this example

enter image description here

Now im not doing a top 2, im just doing a simple entity find, so im a bit at a loss as to why.


Solution

  • You are more than likely using SingleOrDefault or possibly Find. These methods work by selecting the top two rows. It then makes sure that only one row is returned. If more than one element was returned then it will throw an exception.

    How else would it know if there was more than one match?

    // If there were two Products with a ProductName of Widget then the code would throw an exception.
    context.Products.SingleOrDefault(i => i.ProductName == "Widget");
    
    // If there were two Products with a ProductId of 1234 then the code would throw an exception.
    context.Products.Find(1234);