Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Linq Interview Questions and Answers

by Venkatesan M, on Apr 7, 2018 10:50:49 AM

Linq Interview Questions and Answers

Q1. What is LINQ ?

Ans: 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?

Ans: 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?

Ans:

  • 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?

Ans: 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 ?

Ans: 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?

Ans: 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?

Ans: 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.

Ans: 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 ? 

Ans:

  • It should not be used in C# if the same logic is achieved by simple c# coding.
  • linq to sql involves performance overhead hence should be used efficiently.

Q10. What are the advantages of LINQ?

Ans:

  • 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?

Ans: There are some quantifiers in Linq as given below:-
  •  All ( )
  • Contains ( )
  • Any ( )
  • SequenceEqual()

Q12. Differentiate between XElement and XDocument.

Ans: 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.
Ans:

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?

Ans: 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?

Ans: 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()?

Ans: 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 ?

Ans: 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?

Ans:

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

Q19. What are different Join operators used in Linq?

Ans

  • Join
  • GroupJoin

Q20. What are the LINQ components?

Ans:

  • LINQ providers
  • Query operators
  • Language extensions

Q21. Describe the advantages of using LINQ on DataSet.

Ans: 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?

Ans: System.Linq

Q23. What are different Ordering operators used in Linq?

Ans:

  • OrderBy
  • OrderByDescending
  • ThenBy
  • ThenByDescending
  • Reverse

Q24. What are extension methods?

Ans: 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.

Topics:Linq Interview Questions and AnswersInformation Technologies (IT)

Comments

Subscribe

Top Courses in Python

Top Courses in Python

We help you to choose the right Python career Path at myTectra. Here are the top courses in Python one can select. Learn More →

aathirai cut mango pickle

More...