Understanding LINQ in Simple Words

Understanding LINQ in simple words

  • Whenever a new technology or language comes in the market related to data, our developers are bound to learn it for use in their projects.

  • For example, if users want to do a simple join, they need to understand SQL, or if they want to work with XML for query related things, they should understand XQuery.

  • In some companies database (back-end) developers and front-end developers work separately. In that situation our developers are totally dependent on database designers to create even a simple Stored Procedure.

  • For example, if they want to select only the name or age from the database, they need to write a separate Stored Procedure.

  • To simplify this situation, LINQ was introduced in Visual Studio 3.0.

  • Using LINQ, developers can write there own query in code. Therefore, there is no need to contact database developers for simple manipulation.

  • Not only with the database such as SQL, LINQ can be used with other data sources.

  • You can directly select and manipulate data or even transform your data in other formats such as XML using LINQ.

  • We can create a query, not only from SQL DB, but from other data sources such as XML document, ADO.NET, .NET dataset, .NET collection and so on.

  • For using LINQ, you need to first define your data source from which you want to create a query (datasource).

  • After getting your data source, create a query.

  • Then, execute the query.





  • Developers can create queries in code without learning other languages.

  • When you create a LINQ, you will also benefit from IntelliSense and debugging that you can't get in SQL.

  • It means you can track errors at the code level in your LINQ query.

  • With the queries, they can even sort, group or reshape the returning result.

  • If you are familiar with SQL, you may have an idea of query expression such as select name from emp where Id=1, but in LINQ we use "from" first and then where. Then, we write the select.

  • This is done in LINQ because in C# we need to assign a variable first and then use it.

  • Similarly in LINQ, we need to assign a data source before using it.

  • Let us say you want to write the preceding SQL query in LINQ, we can write it as shown below. 
    from e in emp
    Where Id=1
    Select name;

  • When you create LINQ queries, it doesn't mean that the result will be given immediately, just like in SQL, until then your query's execution will be deferred. This means that they only have the information yet nothing is returned. But let us say you want immediate results, you can force the execution of your query by putting a foreach loop immediately after your query or you can use a tolist() or toarray() method at the end of your query.
  • The following are the disadvantages: 1. you cannot create complex queries in LINQ, 2. if there is an error in LINQ, you need to again rebuild your DLL. 3. if your LINQ query is not proper, then it may slow down your project's performance.
Before using LINQ, we should keep the preceding points in mind

Up Next
    Ebook Download
    View all
    Learn
    View all