Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting object using LINQ Min()?

Tags:

c#

.net

linq

I have a LINQ query that ends with .Min(mytable.Id). This will return an integer. I then have to do another query to pull that object from that table. Is there a way to do this without writing two queries?

I could put the .Min() query as a subquery but is that any faster than two separate queries?

like image 464
4thSpace Avatar asked Apr 26 '26 19:04

4thSpace


1 Answers

.OrderBy(m => m.Id).FirstOrDefault();

should do it without any additional dependencies.

Since its deferred, this will be pretty quick.

like image 146
Daniel A. White Avatar answered Apr 29 '26 08:04

Daniel A. White