LINQ Interview Questions and Answers – 1

Q1.  What is LINQ and why to use it?
Ans. LINQ stands for “Language Integrated Query” and pronounced as “LINK”. LINQ was introduced with .NET Framework 3.5 including Visual Studio 2008, C# 3.0 and VB.NET 2008 (VB 9.0). It enables you to query the data from the various data sources like SQL databases, XML documents, ADO.NET Datasets, Web services and any other objects such as Collections, Generics etc. by using a SQL Query like syntax with .NET framework languages like C# and VB.NET.

Why LINQ
LINQ has full type checking at compile-time and IntelliSense support in Visual Studio, since it used the .NET framework languages like C# and VB.NET. This powerful feature helps you to avoid run-time errors.

LINQ also provides a uniform programming model (i.e. common query syntax) to query various data sources. Hence you don’t need to learn the different ways to query different data sources.

Q2.  Which namespace is necessary to use LINQ?
Ans. System.Linq namespace is necessary for writing LINQ queries and to implement it.

Q3.  What are different flavors of LINQ?
Ans. There are following three flavors of LINQ:

1) LINQ to Objects
It enables you to query any in-memory object like as array, collection and generics types. It offers a new way to query objects with many powerful features like filtering, ordering and grouping with minimum code.

2) LINQ to ADO.NET
LINQ to ADO.NET is used to query data from different databases like as SQL Server, Oracle, and others. Further, it can be divided into three flavours:-

I.  LINQ to SQL (DLINQ)
It is specifically designed to work with only SQL Server database. It is an object-relational mapping (ORM) framework that allows 1-1 mapping of SQL Server database to .NET Classes. These classes are automatically created by the wizard based on database table and we can use these classes immediately.

II.  LINQ to Datasets
It is an easy and faster way to query data cached in a Dataset object. This allows you to do further data manipulation operations (like searching, filtering, sorting) on Dataset using LINQ Syntax. It can be used to query and manipulate any database (like Oracle, MySQL, DB2 etc.) that can be query with ADO.NET.

III.  LINQ to Entities
In many ways, it looks like LINQ to SQL. It is an object-relational mapping (ORM) framework that allows 1-1 mapping , 1-many mapping and many-many mapping of a database to .NET Classes. Unlike LINQ to SQL, it can be used to query any database (like Oracle, MySQL, and DB2 etc.) including SQL Server. Now, it is called ADO.NET Entity Framework.

3)  LINQ to XML (XLINQ)
This allows you to do different operations on XML data source like  querying or reading, modifying, manipulating, and saving changes to XML documents.  System.Xml.Linq namespace contains classes for LINQ to XML.

4) Parallel LINQ (PLINQ)
PLINQ was introduced with .NET Framework 4.0. It is a parallel implementation of LINQ to Objects. PLINQ use the power of parallel programming which targets the Task Parallel Library. PLINQ helps you to write a LINQ query which will be execute simultaneously or parallel on different processors.

Q4.  What are advantages of LINQ?
Ans. There are following advantages of using LINQ:

  1. It provides a uniform programming model (i.e. common query syntax) to query data sources (like SQL databases, XML documents, ADO.NET Datasets, Various Web services and any other objects such as Collections, Generics etc.)
  2. It has full type checking at compile-time and IntelliSense support in Visual Studio. This powerful feature helps you to avoid run-time errors.
  3. It supports various powerful features like filtering, ordering and grouping with minimum code.
  4. Its Query can be reused.
  5. It also allows debugging through .NET debugger.

Q5.  What are disadvantages of LINQ?
Ans. There are following disadvantages of using LINQ:

  1. LINQ is not good to write complex queries like SQL.
  2. LINQ doesn’t take the full advantage of SQL features like cached execution plan for stored procedure.
  3. Performance is degraded if you don’t write the LINQ query correctly.
  4. If you have done some changes in your query, you have to recompile it and redeploy its dll to the server.