LINQ follows a unique set of rules and guidelines called syntax. The LINQ syntax is quite an easy one to learn. Here, in this tutorial, we are providing all the basic LINQ syntax.
LINQ provides two main syntaxes: Query Syntax and Method Syntax. You can perform the same operations using both the syntax. And you can write the LINQ query in three different ways. They are as follows:
In LINQ, Query syntax provides a SQL-like way of expressing queries on collections or data sources. It is more declarative and often considered more readable, especially for complex queries.
Query syntax does not support all query operators of LINQ and it must begin with a from
keyword and end with the select
keyword. LINQ query always returns a list of items.
from <range variable> in <IEnumerable<T> or IQueryable<T> Collection>
<Standard Query Operators> <lambda expression>
<select or groupBy operator> <result formation>
Here's an example of using query syntax in LINQ:
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Query syntax to find even numbers, arrange in descending order, and pick the top three
var finalOutput = (from num in numbers
where num % 2 == 0
orderby num descending
select num).Take(3).ToList();
// Display the Output
Console.WriteLine("Query Syntax Output:");
foreach (var num in finalOutput)
{
Console.WriteLine(num);
}
}
}
Query Syntax Output:
10
8
6
In the above example, as you can see the query syntax uses keywords like from
, where
, orderby
, select
, and Take()
to represent each step in the process. In the end, the finalOutput will contain the top three even numbers in descending order: 10, 8, and 6.
In LINQ, Method syntax involves using extension methods and lambda expressions to write concise and fluent SQL queries and all extension methods included in the Enumerable or Queryable static class. It's an alternative to query syntax. Here's an example using method syntax in LINQ:
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Method syntax to find even numbers, arrange in descending order, and pick the top three
var finalOutput = numbers
.Where(num => num % 2 == 0)
.OrderByDescending(num => num)
.Take(3)
.ToList();
// Display the Output
Console.WriteLine("Method Syntax Output:");
foreach (var num in finalOutput)
{
Console.WriteLine(num);
}
}
}
Method Syntax Output:
10
8
6
In this above example, as you can see the Where()
, OrderByDescending()
, and Take()
methods are the extension methods that you're using on your list of numbers to achieve the specific outcome you want. In the end, the finalOutput will contain the top three even numbers in descending order: 10, 8, and 6.
In LINQ, it's possible to use mixed syntax, combining both query syntax and method syntax. This flexibility allows developers to choose the syntax that best fits a particular scenario. Here's an example of mixed syntax in LINQ:
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Mixed Syntax (Query Syntax + Method Syntax)
var MixedSyntax = (from num in numbers
where num % 2 == 0
orderby num descending
select num).Take(3).ToList();
// Display the Output
Console.WriteLine("Mixed Syntax Output:");
foreach (var num in MixedSyntax)
{
Console.WriteLine(num);
}
}
}
Mixed Syntax Output:
10
8
6
In the above example, as you can see that:
from
, where
, orderby
, select
).Take()
, ToList()
) for additional processing.This mixed syntax approach is convenient when the query expression becomes more complex, and it's beneficial to switch between the expressive query syntax and the versatility of method syntax for additional operations.
Remember that both query syntax and method syntax are just different ways to perform the same underlying operations. The choice between them often comes down to personal preference or the specific requirements of the task at hand.
Learn Lambda Expression in the next tutorial