1. What is LINQ?

It stands for Language Integrated Query. LINQ is collection of standard query operators that provides the query facilities into .NET framework language like C# , VB.NET.

4. How LINQ is beneficial than Stored Procedures?

There are couple of advantage of LINQ over stored procedures.

1. Debugging - It is really very hard to debug the Stored procedure but as LINQ is part of .NET, you can use visual studio's debugger to debug the queries.

2. Deployment - With stored procedures, we need to provide an additional script for stored procedures but with LINQ everything gets complied into single DLL hence deployment becomes easy.

3. Type Safety - LINQ is type safe, so queries errors are type checked at compile time. It is really good to encounter an error when compiling rather than runtime exception!

5. What is a Lambda expression?

A Lambda expression is nothing but an Anonymous Function, can contain expressions and statements. Lambda expressions can be used mostly to create delegates or expression tree types. Lambda expression uses lambda operator => and read as 'goes to' operator.

Left side of this operator specifies the input parameters and contains the expression or statement block at the right side.

Example: myExp = myExp/10;

Now, let see how we can assign the above to a delegate and create an expression tree:

delegate int myDel(int intMyNum);
static void Main(string[] args)
{
//assign lambda expression to a delegate:
myDel myDelegate = myExp => myExp / 10;
int intRes = myDelegate(110);
Console.WriteLine("Output {0}", intRes);
Console.ReadLine();

//Create an expression tree type
//This needs System.Linq.Expressions
Expression<myDel> myExpDel = myExp => myExp /10;
}

No te:
The => operator has the same precedence as assignment (=) and is right-associative.

Lambdas are used in method-based LINQ queries as arguments to standard query operator methods such as Where.

7. Why Select clause comes after from clause in LINQ?

The reason is, LINQ is used with C# or other programming languages, which requires all the variables to be declared first. From clause of LINQ query just defines the range or conditions to select records. So that's why from clause must appear before Select in LINQ.

8. Difference between LINQ and Stored Procedures?

There are couple of advantage of LINQ over stored procedures.
1. Debugging - It is really very hard to debug the Stored procedure but as LINQ is part of .NET, you can use visual studio's debugger to debug the queries.
2. Deployment - With stored procedures, we need to provide an additional script for stored procedures but with LINQ everything gets complied into single DLL hence deployment becomes easy.
3. Type Safety - LINQ is type safe, so queries errors are type checked at compile time. It is really good to encounter an error when compiling rather than runtime exception!
4. PreCompiled - Stored Procedures are precompiled, and LINQ queries need to compile before execution. So stored procedures are fast in performance.

9. What are Benefits and Advantages of LINQ?

Benefits and Advantages of LINQ are:
1. Makes it easier to transform data into objects.
2. A common syntax for all data.
3. Strongly typed code.
4. Provider integration.
5. Reduction in work.
6. Performance in the general case doesn't become an issue.
7. Built-in security.
8. LINQ is declarative.

10. What are the three main components of LINQ or Language INtegrated Query?

1. Standard Query Operators
2. Language Extensions
3. LINQ Providers

Download Interview PDF

11. How are Standard Query Operators implemented in LINQ?

Standard Query Operators are implemented as extension methods in .NET Framework. These Standard Query Operators can be used to work with any collection of objects that implements the IEnumerable interface. A class that inherits from the IEnumerable<t> interface must provide an enumerator for iterating over a collection of a specific type. All arrays implement IEnumerable<t>. Also, most of the generic collection classes implement IEnumerable<t> interface.

12. How are Standard Query Operators useful in LINQ?

Standard Query Operators in LINQ can be used for working with collections for any of the following and more.
1. Get total count of elements in a collection.
2. Order the results of a collection.
3. Grouping.
4. Computing average.
5. Joining two collections based on matching keys.
6. Filter the results

13. List the important language extensions made in C# to make LINQ a reality?

1. Implicitly Typed Variables
2. Anonymous Types
3. Object Initializers
4. Lambda Expressions

14. What is the purpose of LINQ Providers in LINQ?

LINQ Providers are a set of classes that takes a LINQ query and dynamically generates a method that executes an equivalent query against a specific data source.

15. What are the four LINQ Providers that .NET Framework ships?

1. LINQ to Objects - Executes a LINQ query against a collection of objects
2. LINQ to XML - Executes an XPATH query against XML documents
3. LINQ to SQL - Executes LINQ queries against Microsoft SQL Server.
4. LINQ to DataSets - Executes LINQ queries against ADO.NET DataSets.

16. Write a program using LINQ to find the sum of first 5 prime numbers?

class Program
{
static void Main()
{
int[] MyPrimeNumbers = {1,2,3,5,7};
// Use the Count() and Sum() Standard Query Operators to
// compute the count and total sum respectively
Concole.WriteLine("The sum of first {0} prime number is {1}",
MyPrimeNumbers.Count(), MyPrimeNumbers.Sum());
}
}

17. What is Difference between XElement and XDocument?

Both 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.
example:

XDocument d = new XDocument(
new XComment("hello"),
new XElement("book",
new XElement("bookname", "ASP.NET"),
new XElement("authorname", "techmedia"),
)
);

18. What are Quantifiers?

They are LINQ Extension methods which return a Boolean value
1) All
2) Any
3) Contains
4) SequenceEqual
example:
int[] arr={10,20,30};
var b=arr.All(a=>a>20);
-------------------------------------------
Output:
b will return False since all elements are not > 20.

19. What is the benefit of using LINQ on Dataset?

The main aim of using LINQ to Dataset is to run strongly typed queries on Dataset.
Suppose we want to combine the results from two Datasets, or we want to take a distinct value from the Dataset, then it is advisable to use LINQ.
Normally you can use the SQL queries to run on the database to populate the Dataset, but you are not able to use SQL query on a Dataset to retrieve a particular values. To get this you need to use ADO.NET functionalities. But, in case of LINQ, it provides more dignified way of querying the Dataset and provides some new features as compared to ADO.NET.

20. Which classs extension methods are used in LINQ to SQL?

1. System.Linq.Enumerable
2. System.Linq.Queryable
3. None of these

21. Which assembly represents the core LINQ API?

System.Query.dll assembly represents the core LINQ API.

22. What is the use of System.Data.DLinq.dll?

System.Data.DLinq.dll provides functionality to work with LINQ to SQL.

23. What is the use of System.XML.XLinq.dll?

System.XML.XLinq.dll contains classes to provide functionality to use LINQ with XML.