Поиск  
Always will be ready notify the world about expectations as easy as possible: job change page
May 16, 2022

Best LINQ performance

Автор:
Hussein NM
Источник:
Просмотров:
1704

In this article, I will show the most performant way to write queries for a collection of objects in .Net Core. The article discusses the common methods of LINQ and their comparison. After reading this article, you may have to review your project code!

Comparisons were made between query syntax, called LINQ, and method syntax called LAMBDA.

💡 Connect with me on LinkedIn: https://www.linkedin.com/in/hussein-nm/

✨ Where()

LINQ_Where (Method name):

LAMBDA_Where:

🚀 Performance

Which one do you think is faster?

In this simple performance query, both methods are similar.

✨ Any()

LINQ_Any:

LAMBDA_Where_Any:

LAMBDA_Any:

🚀 Performance

Using Any() as the LAMBDA_Any method is faster.

✨ First()

LINQ_First:

LAMBDA_Where_First:

LAMBDA_First:

🚀 Performance

Using First() as the method a LAMBDA call is faster.

✨ FirstOrDefault()

LINQ_FirstOrDefault:

LAMBDA_Where_FirstOrDefault:

LAMBDA_FirstOrDefault:

🚀 Performance

Using FirstOrDefault() as the method a LAMBDA call is faster.

✨ Last()

LINQ_Last:

LAMBDA_Where_Last:

LAMBDA_Last:

🚀 Performance

Using Last() as the method a LAMBDA call is faster.

✨ LastOrDefault()

LINQ_LastOrDefault:

LAMBDA_Where_LastOrDefault:

LAMBDA_LastOrDefault:

🚀 Performance

Using LastOrDefault() as the method a LAMBDA call is faster.

Conclusion

As shown in this article, you should always use Any(), First(), FirstOrDefault(), Last(), LastOrDefault, or Where() at the beginning of a LAMBDA statement.

Похожее
Feb 2
Author: Achref Hannachi
IntroductionLINQ (Language Integrated Query) is a powerful feature in C# that allows developers to perform complex queries on collections and databases using a syntax that is both expressive and readable. However, writing LINQ queries efficiently is essential to ensure that...
Aug 11, 2021
Author: Mel Grubb
Code GenerationCode generation is a great way to apply patterns consistently across a solution or to create multiple similar classes based on some outside input file, or even other classes in the same solution. The tooling has changed over the...
Mar 18
Author: Erik Pourali
Imagine crafting a library app where users effortlessly find books by title, author, or genre. Traditional search methods drown you in code. But fear not! Dynamic Querying in C# saves the day.In our tale, crafting separate search methods for each...
Sep 28, 2022
Author: Edis Nezir
Identify if-else statements as a problem in your code.If-else statements can be problematic if not used correctly. They can be difficult to read and can lead to code that is difficult to maintain. When used incorrectly, if-else statements can also...
Написать сообщение
Почта
Имя
*Сообщение


© 1999–2024 WebDynamics
1980–... Sergey Drozdov
Area of interests: .NET Framework | .NET Core | C# | ASP.NET | Windows Forms | WPF | HTML5 | CSS3 | jQuery | AJAX | Angular | React | MS SQL Server | Transact-SQL | ADO.NET | Entity Framework | IIS | OOP | OOA | OOD | WCF | WPF | MSMQ | MVC | MVP | MVVM | Design Patterns | Enterprise Architecture | Scrum | Kanban
Три дороги для программиста: эксперт, руководитель, основатель
Performance review, ачивки и погоня за повышением грейда — что может причинить боль сотруднику IT-компании?
14 вопросов об индексах в SQL Server, которые вы стеснялись задать
Асинхронный JavaScript: изучаем Async/Await, Callbacks и Promises
Типичные взаимные блокировки в MS SQL и способы борьбы с ними
Универсальный ускоритель инженера: как расти быстрее с помощью проектов
Инструкция по оформлению профиля на GitHub
Мудрёный код — пожалуй, худший выбор
Развенчание мифа о собственной продуктивности программистов
Как мы столкнулись с версионированием и осознали, что вариант «просто проставить цифры» не работает
LinkedIn: Sergey Drozdov
Boosty
Donate to support the project
GitHub account
GitHub profile