Thursday 27 November 2014

LINQ

What is LINQ ?

1. LINQ stands for Language of Integrated Query.It is a new technology developed by Microsoft.
2. Its primary function is to add native data query capability to .net using similar syntax like SQL.
3. It is a simple query operator which allow Traversing, Filtering and projection operation in .net based programming language.
4. It Simplifies the situation by consistent syntax for working data across the different datasourse and there formats.
5. It allows the query expression to benefit from rich metadata, Compile Time error checking and intelligence that previously available only to the imperative code.

Why LINQ is beneficial than Store Procedure?

1. Debugging : Since LINQ is part .Net, so we can use the debugger of visual Studio t debug the query.But to debug the stored procedure is very hard.
2. Development : Deployment becomes easy with LinQ, because its(LINQ) is a .Net component,so its compiles everything into a single DLL File.
3. Type Safety : Since, In LINQ query errors are type checked at compile time.It is really good to encounter an error when compiling rather than run time exception.

Difference between FirstOrDefault and single or default

If your result set returns 0 records:

SingleOrDefault returns the default value for the type (e.g. default for int is 0)
FirstOrDefault returns the default value for the type

If you result set returns 1 record:

SingleOrDefault returns that record
FirstOrDefault returns that record

If your result set returns many records:

SingleOrDefault throws an exception
FirstOrDefault returns the first record
Conclusion:

If you want an exception to be thrown if the result set contains many records, use SingleOrDefault.

If you always want 1 record no matter what the result set contains, use FirstOrDefault.


LINQ Single vs SingleOrDefault vs First vs FirstOrDefault

Single()SingleOrDefault()First()FirstOrDefault()
DescriptionReturns a single, specific element of a sequenceReturns a single, specific element of a sequence, or a default value if that element is not foundReturns the first element of a sequenceReturns the first element of a sequence, or a default value if no element is found
Exception thrown whenThere are 0 or more than 1 elements in the resultThere is more than one element in the resultThere are no elements in the resultOnly if the source is null (they all do this)
When to useIf exactly 1 element is expected; not 0 or more than 1When 0 or 1 elements are expectedWhen more than 1 element is expected and you want only the firstWhen more than 1 element is expected and you want only the first. Also it is ok for the result to be empty
What is a query expression in LINQ? 
A LINQ query, also known as a query expression, it consists of a combination of query clauses that identify the data sources for the query. It includes instructions for sorting, filtering, grouping, or joining to apply to the source data. The LINQ query expressions syntax is similar to the SQL syntax. It specifies what information should be retrieved from the data source.

Example of Searching in Collections:

The Old Way
Find the names of all customers from New Delhi.
List<string> LstDelhi = new List<string>();
foreach (Customer c in customers) {
if (c.City == “New Delhi”) {
LstDelhi.add(c.Name);
}
}

The LINQ Way
string[] LstDelhi =from c  in customers
   where c.City == “New Delhi”
   select c.Name;

How LINQ query executes? Or What are the basic steps to execute LINQ query?
The following are the three basic steps to execute a LINQ query:
1. Obtain the data source (Data source can be either an SQL database or an XML file).
2. Create a query .
3. Execute the query

What is the DataContext class and how is it related to LINQ?
After you add a LINQ to SQL Classes item to a project and open the O/R Designer, the empty design surface represents an empty DataContext class ready to be configured. 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.

What does DISTINCT clause in a LINQ query? Also write a simple query.
The DISTINCT clause returns the result set without the duplicate values.

 var query_distinct = (from c in svcContext.ContactSet
                       select c.LastName).Distinct();
 foreach (var c in query_distinct)
 {
  System.Console.WriteLine(c);
 }


What are lambda expressions in LINQ? 
A lambda expression is a function without a name that calculates and returns a single value. All lambda expressions use the lambda operator =>, which read as goes to. The left side of the lambda operator specifies the input parameters and the right side holds the expression or statement block.

What are the different implementations of LINQ? 
The different implementations of LINQ are:
LINQ to SQL - Refers to a component of.NET Framework version 3.5 that provides a run-time infrastructure to manage relational data as objects.
LINQ to DataSet - Refers to a component that makes it easier and faster to query over data cached in a DataSet object.
LINQ to XML - Provides an in-memory XML programming interface.
LINQ to Objects - Refers to the use of LINQ queries with any IEnumerable or IEnumerable(T) collection directly, without the use of an intermediate LINQ provider or API, such as LINQ to SQL or LINQ to XML.

What is the difference between the Select clause and SelectMany() method in LINQ? 
Both the Select clause and SelectMany() method are used to produce a result value from a source of values. The difference lies in the result set. The Select clause is used to produce one result value for every source value. The result value is a collection that has the same number of elements from the query. In contrast, the SelectMany() method produces a single result that contains a concatenated collection from the query.



No comments:

Post a Comment