What are some of the "best practices" of linq,

Looking at using linq throughout my next project to ease staffing. I used to immerse myself in the linq world, I would like to get tips on the best principles of using linq. I have not decided on EF and linq before sql

+4
source share
4 answers

First, you need to determine what you mean by "LINQ". I will guess the rest of the question about what you are asking about Linq for SQL.

If you mean Linq to SQL, and you are starting a new project, I would say that the first best practice is to not do this and use the Entity Framework in .NET. It is far superior to EF 1.0, as well as Linq to SQL. Since Visual Studio 2010 and .NET 4 were released, Linq to SQL should be avoided for all new projects.

  • The best practice I can give you is to choose the right database model. Correctly normalize your database. Correctly configure and index your database. Create an EF model from this well-designed database and you will be happy. If you try to cut corners or somehow model the database, as if it were some kind of repository of non-relational objects, your life will be full of pain and suffering.

  • Make sure everyone in your team understands the concept of deferred execution and how it relates to how your requests are actually generated and executed.

  • Understand the difference between IQueryable <> and IEnumerable <>. This view is from a previous point, but when it comes to compiling Linq queries, understanding how IQueryable <> works can make your life a lot easier.

  • Understand lazy loading and how it will affect the performance of your queries. Use it when necessary, and learn how to strategically aim at loading objects for better performance.

  • Understand the frequency of each request and what the impact will be on the load. Some of your most high-frequency queries can often be useful when compiled, but most of the time you do not need to do this.

+7
source

Thank you for listing instead of parsing for data types.

I found this very important, as it can significantly improve the readability of your queries.

0
source

To avoid being bitten by a lazy assessment, check out this Jon Wagner's Best Practices for Linq Listings and Queries blog post , which I quote here:

  • Use "Fluent C #" rather than language extensions.
  • "Print" LINQ chains as soon as they are built (if they are reused).
  • Only return open chains if you want to enable linking.
  • Make sure that the semantics for the "open" return circuit are clear.
  • If you are returning an open chain, make sure that the internal code does not contain side effects.
0
source

Source: https://habr.com/ru/post/1307104/


All Articles