Saturday, 21 January 2017

Introduction to LINQ Standard Query Operators Vs SQL

Language Integrated Query (LINQ) provides a set of operators called as standard query operators for simplifying data access and manipulation across data sources.
The standard query operator is a set of methods. These methods operate on sequences. A sequence is an object whose type implements either the IEnumerable interface or the IQueryable interface. These operators can be used to filter, project, join, order, and group data in LINQ query.

Related Articles

  1. Language Integrated Query (LINQ) tutorials for beginners

Query Operators - SQL vs LINQ

Following table shows the operators based on the functionality and also corresponding SQL operators.

FunctionalitySQL OperatorsLINQ Operators
ProjectionSelectSelect
SelectMany
Read More...
RestrictionWhereWhere
Read More...
OrderingOrderByOrderBy
Read More...
ThenBy
Read More...
GroupingGroupByGroupBy
Read More...
QuantifiersAny
All
Any
All
Contains
Read More...
PartitioningNot availableSkip
SkipWhile
Read More...
Take
TakeWhile
Read More...
SetsDistinct
Union
Intersect
Distinct
Union
Intersect
Except
ElementsNot availableFirst
FirstOrDefault
ElementAt
AggregationCount
Sum
Min
Max
Average
Count
Sum
Min
Max
Average

Writing Query – SQL vs LINQ

Examples

Display the names and phone numbers of all customers residing in Mumbai city.

SQL Syntax

SQL Syntax

LINQ Syntax

LINQ Syntax

The following code example demonstrates how the standard query operators can be used to obtain information about a sequence.

string sentence = "the quick brown fox jumps over the lazy dog";
// Split the string into individual words to create a collection.
string[] words = sentence.Split(' ');

// Using query expression syntax.
var query = from word in words
            group word.ToUpper() by word.Length into gr
            orderby gr.Key
            select new { Length = gr.Key, Words = gr };

// Using method-based query syntax.
var query2 = words.
    GroupBy(w => w.Length, w => w.ToUpper()).
    Select(g => new { Length = g.Key, Words = g }).
    OrderBy(o => o.Length);

foreach (var obj in query)
{
    Console.WriteLine("Words of length {0}:", obj.Length);
    foreach (string word in obj.Words)
        Console.WriteLine(word);
}

/*output:
Words of length 3:
THE
FOX
THE
DOG
 
Words of length 4:
OVER
LAZY
 
Words of length 5:
QUICK
BROWN
JUMPS 
*/
Share:

0 comments:

Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive

BUY FROM AMAZON