Friday 17 March 2017

Linq interview questions Part-2

1. Ques: What is difference between IEnumerable and IQueryable ?
Answer:
Difference between IEnumerable and IQueryable
IEnumerable
  • IEnumerable exists in System.Collections Namespace.
  • IEnumerable can move forward only over a collection, it can’t move backward and between the items.
  • IEnumerable is best to query data from in-memory collections like List, Array etc.
  • While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.
  • IEnumerable is suitable for LINQ to Object and LINQ to XML queries.
  • IEnumerable supports deferred execution.
  • IEnumerable doesn’t supports custom query.
  • IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.
  • Extension methods supports by IEnumerable takes functional objects.
IQueryable
  • IQueryable exists in System.Linq Namespace.
  • IQueryable can move forward only over a collection, it can’t move backward and between the items.
  • IQueryable is best to query data from out-memory (like remote database, service) collections.
  • While query data from database, IQueryable execute select query on server side with all filters.
  • IQueryable is suitable for LINQ to SQL queries.
  • IQueryable supports deferred execution.
  • IQueryable supports custom query using CreateQuery and Execute methods.
  • IQueryable support lazy loading. Hence it is suitable for paging like scenarios.
  • Extension methods supports by IQueryable takes expression objects means expression tree.

2. Ques: What is the difference between Linq to Object and Linq to SQL ?
Answer:
LINQ to Objects queries operate on IEnumerable collections. The query iterates through the collection and executes a sequence of methods (for example, Contains, Where etc) against the items in the collection. 

LINQ to SQL queries operate on IQueryable collections. The query is converted into an expression tree by the compiler and that expression tree is then translated into SQL and passed to the database. 


3. Ques: What is the difference between First() and Single() extension methods in LINQ ?
Answer:
First(): Use Single to retrieve the first (and only) element from a sequence that should contain one element and no more. If the sequence has more than on element your invocation of Single will cause an exception to be thrown since you indicated that there should only be one element.

Single(): Use First to retrieve the first element from a sequence that can contain any number of elements. If the sequence has more than on element your invocation of First will not cause an exception to be thrown since you indicated that you only need the first element in the sequence and do not care if more exist.


4. Ques: What are Quantifiers in Linq ?
Answer:
 Quantifier Operators return the Boolean value (either True or false) if some or all the elements in a sequence satisfy a condition.

1)All
2)Any
3)Contains
4)SequenceEqual

example:
int[] arr={10,20,30};
var b=arr.All(a=>a>20);


5. Ques:  What are the different implementations of LINQ ?
Answer:
Following are the different implementations of LINQ:

LINQ to SQL : This component was introduced in .Net framework version 3.5 that gives a run-time mechanism to manipulate relational data as objects.

LINQ to DataSet : This component facilitates to run simpler and faster query over data which is cached in the DataSet object.

LINQ to XML : Provides an in-memory XML programming interface.

LINQ to Objects : It provides the use of LINQ queries with any IEnumerable or IEnumerable(T)collection directly, without the use of an middle LINQ provider or API, like LINQ to SQL or LINQ to XML.


6. Ques:  What is DataContext class ?
Answer:
The DataContext class is a LINQ to SQL class that acts as a conduit between a SQL Server database and the LINQ to SQL entity classes mapped to that database. This class contains the connection string information and the methods for connecting to a database and manipulating the data in the database. It is configured with connection information provided by the first item that is dragged onto the design surface.

DataContext class performs the following three tasks:
  • Create connection to database.
  • It submits and retrieves object to database.
  • Converts objects to SQL queries and vice versa.

7. Ques:  What is a LinqDataSource control?
Answer:
The LinqDataSource control enables you to use LINQ. in an ASP.NET Web page by setting the properties in the markup text. You can use the control retrieve or modify data. It is similar to the SqIDataSource andObjectDataSource controls in the sense that it can be used to declaratively bind other ASP.NET controls on a page to a data source. The difference is that instead of binding directly to a database or to a generic class, theLinqDataSource control is designed to bind a LINQ enabled data model.


8. Ques: What is Action in LINQ?
Answer:
Action are generic delegates provided by base class library of .NET. In Action delegate we can only store those methods that have only input parameters and void return types. We can specify upto 16 parameters.
Below is the example of Action delegate:

func f1 = (a, b) => a + b;

Action<int> printAction = (a) => Console.WriteLine(a);
printAction(f1(1, 3));


9. Ques: What is Predicate delegate in LINQ ?
Answer:
Predicate is a feature that returns true or false.This concept has come in .net 2.0 framework. Predicate is a delegated provided by base class library of NET.
In Predicate delegate we can only store those method which have one input parameter and a bool return type.
Predicate delegates are mainly used in filtering scenarios in LINQ where we have to filter some list.
It is being used with lambda expression (=>). It takes generic type as an argument.
It allows a predicate function to be defined and passed as a parameter to another function

 Example of Predicate delegate given bellow

Predicate<string> isStringStartWithMChar = (a) => a.StartsWith("m");
Console.WriteLine(isStringStartWithMChar("msmd"));


10. Ques:  What is Func Delegates ?
Answer:
Func delegates are pointers to methods that take one or more parameters and must return a value. There are many overloaded Func delegates. Please see the documentation for a better understanding of those. In this article let's have a general idea of how to use it programmatically.

Usage
  Generelly it's used with anonymous methods.
Example:
Func<int, int, int, long> add = delegate(int a, int b, int c) { return a + b + c; };
 Console.WriteLine(add(2, 3, 4)); 

No comments:

Post a Comment

Thank you for comment