Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some linq "best practices" [closed]

Tags:

c#

linq

Looking to use linq throughout my next project to ease some of the hard labor. Before, I dive into the linq world I would like some advice on best prctices on using linq. I am still undecided on EF and linq to sql

like image 935
Luke101 Avatar asked Apr 16 '10 03:04

Luke101


People also ask

Is LINQ to SQL obsolete?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.

What is LINQ in which scenarios it should be used?

LINQ is a data querying API that provides querying capabilities to . NET languages with a syntax similar to a SQL. LINQ queries use C# collections to return data. LINQ in C# is used to work with data access from sources such as objects, data sets, SQL Server, and XML. LINQ stands for Language Integrated Query.

What is better than LINQ?

A stored procedure is the best way for writing complex queries as compared to LINQ. Deploying a LINQ based application is much easy and simple as compared to stored procedures based.


1 Answers

First off, you need to define what you mean by "LINQ". I'm going to guess by the rest of the question that you're asking about Linq to 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 it, and use the Entity Framework in .NET. It's far superior to EF 1.0 as well as Linq to SQL. Ever since Visual Studio 2010 and .NET 4 were released, Linq to SQL should be avoided for all new projects.

  • The biggest Best Practice I can give you is to get the database model right. Properly normalize your database. Properly tune and index your database. Build the EF model off of that properly created database and you will be happy. If you try to cut corners, or you somehow model the database as if it were some sort of non-relational object storage, your life will be one full of pain and suffering.

  • Make sure everyone on your team understands the concept of deferred execution and how that relates to how your queries are actually generated and executed.

  • Understand the difference between IQueryable<> and IEnumerable<>. This sort of relates to the previous point, but when it comes to composing 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 appropriate, and learn how to eager-load objects strategically for the best performance.

  • Understand the frequency of each query and what the impact will be on load. Some of your most high-frequency queries can often benefit by being compiled, but most of the time you don't need to do this.

like image 107
Dave Markle Avatar answered Oct 31 '22 15:10

Dave Markle