I've been recently looking into my web app performance and found some LINQ queries that I'm not sure if changing them would improve performance.
Basically the current code looks like:
var result = _carsRepository.GetAll()
.Where(x => x.Name == input.Name)
.FirstOrDefault();
if (result != null)
{
throw new Exception("test");
}
I'm thinking of changing it to:
var result = _carsRepository.GetAll()
.Where(x => x.Name == input.Name)
.Any();
if (result)
{
throw new Exception("test");
}
In my understanding the first query would return an actual entity, which I don't need because I only want to know whether the record with the same name already exists in the database. The second query returns just a bool.
I'd be thankful for any comments.
EDIT: I could be running the query on EF db context, so please ignore that. The current repository is a generic Abp.Domian.Repository. GetAll() returns IQueryable
where
, if not, you probably need to indexName
– TheGeneral Sep 27 '18 at 8:25Where
is exactly the same in both queuries. – Tim Schmelter Sep 27 '18 at 8:26.Any()
expresses the intent of what you're doing more clearly. – jeroenh Sep 27 '18 at 8:26