Linq Interview Questions and Answers

Q1. What is LINQ ?
LINQ stands for language integrated query. Its a part of .Net 3.5 framework.It provides quering capabilities in .Net languages.it intergrates databse model into programming model.logic can be used to retrieve data with sql like statements in C#.

Q2. What are data providers?
Data providers allows us to query different data sources using LINQ.There are different data providers for different data sources such as:

  • LINQ to Objects
  • LINQ to SQL
  • LINQ to XML
  • LINQ to ADO.NET

LINQ providers converts the LINQ queries to the queries which specific data source understands.

Q3. What is Lambda VS Anonymous Method?

  • The Lambda Expression is almost similar to anonymous method which was introduced with .NET 2.0.
  • The Lambda Expression provides a more concise and functional syntax for writing the anonymous method.
  • There are no basic Difference between them.Means Lambda Expression is an anonymous function.
  • In Lambda Expression ,syntax elements are not required,which is automatically managed by the compiler.

Q4. Could you differentiate between Statement Lambda and Expression Lambda?
For constructing expression trees Expression Lambda is used but we cannot use Statement Lambda for this purpose. Further, statement lambda is not supported by expression providers like LINQ to SQL.

Q5. What are diff area in which LINQ can be used ?
LINQ is divided into 3 categories —
a. linq to objects–this includes mainly quering collections which implemet IEnumerable interface.
b. linq to ADO.NET

  • linq to SQL
  • linq to DataSet
  • linq to Entities(ORM model)

c. linq to XML

Q6. What are query expressions?
Query expressions are used to write LINQ queries in similar way we write SQL queries.Below is a simple query expression:

IEnumerable<int> result = from item in listItmes
where item.name > “TV”
select item.price;

Q7. What are different Methods to write LINQ Query?
The above query finds the price of the product “TV”.

  • Query Syntax
  • Method Syntax
  • Mixed Syntax

Q8. Explain the role of DataContext classes in LINQ.
DataContext class can be explained as a bridge between a SQL Server database and the LINQ to SQL. It establishes connection to database for accessing the database. Communicate with database by submitting and retrieving objects and then change these objects to SQL queries. In a simpler way, it contains the connection string information, methods to connect to the database and manipulating the data in the database. With the help of data context, we can perform select, insert, update and delete operations over the data in the database.

Q9. When not to use LINQ ? 
a.It should not be used in C# if the same logic is achieved by simple c# coding.
b.linq to sql involves performance overhead hence should be used efficiently.

Q10. What are the advantages of LINQ?

  • It allows to write query using the same language we use to develop application
  • It allows to find errors at compile time
  • LINQ can be used to query any data source for which LINQ provider exists.

Q11. What are quantifiers in Linq?
There are some quantifiers in Linq as given below:-
1. All ( )
2. Contains ( )
3. Any ( )
4. SequenceEqual()

Q12. Differentiate between XElement and XDocument.
Both XElement and XDocument are the classes defined by System.Xml.Linq namespace. XElement class represents an XML fragment. XDocument class represents an entire XML document with all associated meta-data. XDocument has a Declaration i.e. root whereas XElement is a single node.

Q13. Use LINQ to query the numbers greater than 5 from a collection of array.
int[] nums = new int[] {0,4,2,6,3,8,3,1};
var result = from n in nums
where n > 5
orderby n             //orders in asc order
select n;

Q14. What is Lambda expression?
Lambda expression is like anonymous function.It does not not have a body and return type.It can assigned to a delegate.We use lambda expression in LINQ to assign it to a delegate.
It has the following form:
input arguments=>body
we can assign a lambda expression to a delegate which returns int as:
delegateMult del= x => x * x;

Q15. What are different Aggregate operators used in Linq?
we can call above delegate as:del(10);
There are some aggregate operators used in Linq as given below:-

  • Aggregate
  • Average
  • Count
  • LongCount
  • Min
  • Max
  • Sum

Q16. Could you tell the difference between XElement.Load() and XDocument.Load()?
For loading everything under the top-level element, we have to use XElement.Load () and for loading any markup before the top-level element, we have to use XDocument.Load ().

Q17. What is ORM ?
it stands for object relational mapping wherein it provides the user with the ability to change and manipulate the objects without having to worry about how the objects are related to their datasource.

Q18. What are some LINQ providers?

  • LINQ to Objects
  • LINQ to SQL
  • LINQ to XML
  • LINQ to JSON
  • LINQ to Sharepoint

Q19. What are different Join operators used in Linq?
Join

  • GroupJoin

Q20. What are the LINQ components?
1.LINQ providers
2.Query operators
3.Language extensions

Q21. Describe the advantages of using LINQ on DataSet.
LINQ to DataSet is a very easy way to write query from the programming language instead of using a separate query languageI. It is useful to run queries on multiple datasets. Through SQL query, we are not able to retrieve a particular value from the dataset. LINQ is an efficient way to perform data manipulation operations (like searching, filtering, sorting) on Dataset.

Q22. Which namespace defines LINQ extension methods?
System.Linq

Q23. What are different Ordering operators used in Linq?

  • OrderBy
  • OrderByDescending
  • ThenBy
  • ThenByDescending
  • Reverse

Q24. What are extension methods?
Extension methods allows us to add new methods to an existing type.When we use extension methods we add new methods without modifying the existing type.

Summary
Review Date
Reviewed Item
Nice collection of interview questions.
Author Rating
51star1star1star1star1star