Thursday, 16 March 2017

Linq interview questions Part-1

1. Ques: What is LINQ ? What are the types of LINQ ?  What are the Advantages and Disadvantages of LINQ ?
Answer:
LINQ stands for Language Integrated Query, is the collection of standard query operators which provides query facilities into.NET framework language like C#, VB.NET.

Types of  LINQ  are 
  • LINQ to Objects
  • LINQ to XML
  • LINQ to Dataset
  • LINQ to SQL
  • LINQ to Entities
Advantages
  • Quick turn around for development
  • Queries can be dynamically
  • Tables are automatically created into class
  • Columns are automatically created into properties
  • Relationship are automatically appeaded to classes
  • Lambda expressions are awesome
  • Data is easy to setup and use
Disadvantages
  • No clear outline for Tiers
  • No good way of view permissions
  • Small data sets will take longer to build the query than execute
  • There is an overhead for creating queries
  • When queries are moved from sql to application side, joins are very slow
  • DBML concurrency issues

2. Ques: What are Lambda expressions ? 
Answer:
A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types. 
All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. 

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 and Where(IQueryable, String, array[]).

Ex:
class Program
  {
     delegate int del(int i);
     static void Main(string[] args)
     {
        del myDelegate = y => y * y;
        int j = myDelegate(5);
        Console.WriteLine(j);
        Console.ReadLine();
     }
  }


3. Ques:  What is PLINQ?
Answer:
PLINQ stands for Parallel Language Integrated Query. It is the parallel implementation of LINQ, in which a query can be executed by using multiple processors. PLINQ ensures the scalability of software on parallel processors in the execution environment. It is used where data grows rapidly, such as in telecom industry or where data is heterogeneous.

PLINQ also supports all the operators of LINQ. In addition, you can query 'collections by using PLINQ. It can also run several LINQ queries simultaneously and makes use of the processors on the system. Apart from this, PLINQ uses parallel execution, which helps in running the queries quickly. Parallel execution provides a major performance improvement to PLINQ over certain types of legacy code, which takes too much time to execute.


4. Ques:  What is the difference between the Take and Skip clauses?
Answer:
Take clauses
The Take clause will give you a predefined number of elements.
if you want to return two elements from an array then you can use Take clause.

Skip clauses
The Skip clause will skip the predefined number of elements in the query and returns the remaining elements.
if you want to skip first five strings in an array of strings and want to retrieve the remaining string then you can use Skip clause.


5. Ques:  What is the difference between the Select clause and SelectMany() method in LINQ?
Answer:
Difference between the Select clause and SelectMany clause given as
 Select clause
  • Select operator is used to select value from a collection and 
  • Select operator produce one result value for every source value 
Select Many 
  • SelectMany operator is used to select values from a collection of collection i.e. nested collection.
  • SelectMany produce a single result that contains a concatenated value for every source value.
  • Actually, SelectMany operator flatten IEnumerable<IEnumerable<T>> to IEnumrable<T> i.e. list of list to list.

6. Ques: What are the advantages of LINQ over stored procedure?
Answer:
Advantages of LINQ over stored procedure?

  • Debugging – LINQ supports .Net debugger, so we can easily debug a LINQ query using .NET debugger but it is not supported by SQL stored procedure so it is hard to debug the stored procedure.
  • Deployment – To deploy stored procedure it is necessary to write one more script to run them, while LINQ will complie by a single DLL statement and so the deployment will be simple.
  • Type Safety - As LINQ supports type safety so errors can be type checked in LINQ queries in compile time.It is better to use LINQ as it enable us to identify the errors while compilation rather than runtime execution.

7. Ques: What is the difference between first() and single() extension methods in linq ?
Answer:

First()
It returns First element value from collection of elements or a sequence. It also returns first element of sequence when you specified condition. If result has no element than it will throws InvalidOperationException.

FirstOrDefault( )
FirstOrDefault()  is just like First() except that, if no element match the specified condition than it returns default value of underlying type of generic collection. It does not throw InvalidOperationException if no element found. But collection of element or a sequence is null than it throws an exception.


8. Ques: What is difference between LINQ and Stored Procedures?
Answer:

  • Stored procedures normally are faster as they have a predictable execution plan. Therefore, if a stored procedure is being executed for the second time, the database gets the cached execution plan to execute the stored procedure.
  • LINQ supports type safety against stored procedures.
  • LINQ supports abstraction which allows framework to add additional improvements like multi threading. It’s much simpler and easier to add this support through LINQ instead of stored procedures.
  • LINQ allows for debugging using .NET debugger, which is not possible in case of stored procedures.
  • LINQ supports multiple databases against stored procedures which need to be re-written for different databases.
  • Deploying LINQ based solution is much simpler than a set of stored procedures

No comments:

Post a Comment

Thank you for comment