Learn why query syntax is preferred in LINQ. Discover a better approach using Query Syntax on Day 27 of our 30-Day .NET Challenge.
Introduction
The article demonstrates the use of query and method syntax for writing LINQ queries. In addition to that, highlights why the query syntax is preferred over method syntax in case of complex queries.
Learning Objectives
What is Query and Method Syntax
Why Query Syntax is preferred
Prerequisites for Developers
Basic understanding of C# programming language.
Familiar with LINQ
Getting Started
Before diving in let's understand each syntax for LINQ.
Method Syntax
It is one of the syntaxes for writing queries which make use of extension methods and lambda expressions.
var query = items.Where(item => item.IsActive)
.Select(item => new { item.Name, item.Id });
As the queries become more complex in nature, the syntax is not readable.
Query Syntax
It is the second type of syntax for writing queries using complex filtering, grouping and join operations.
var query = from item in items
where item.IsActive
select new { item.Name, item.Id };
The aforementioned syntax is quite similar to SQL query structure, which makes it more understandable, readable and maintainable.
Why Query Syntax is preferred
Consider an example wherein we need to join two collections, filter them and map them into a new type. Let's solve the above problem statement using both of the syntaxes.
Method Syntax
The following implementation seems more dense and complex.
var query = items.Join(otherCollection,
item => item.Key,
other => other.Key,
(item, other) => new { item.Name, other.Description })
.Where(x => x.Description.Contains("specific keyword"))
.Select(x => new { x.Name, x.Description });
Query Syntax
Rewriting the same query using query syntax makes it more readable and understandable.
var query = from item in items
join other in otherCollection on item.Key equals other.Key
where other.Description.Contains("specific keyword")
select new { item.Name, other.Description };
Complete Code
Create another class named QueryVsMethod and add the following code snippet
public static class QueryVsMethod
{
static List<Item> items = new List<Item>
{
new Item { Id = 1, Name = "Item1", IsActive = true },
new Item { Id = 2, Name = "Item2", IsActive = false },
new Item { Id = 3, Name = "Item3", IsActive = true }
};
public static void QuerySyntax()
{
// Method Syntax
var methodSyntaxQuery = items.Where(item => item.IsActive)
.Select(item => new { item.Name, item.Id });
Console.WriteLine("Method Syntax Results:");
foreach (var item in methodSyntaxQuery)
{
Console.WriteLine($"Name: {item.Name}, Id: {item.Id}");
}
}
public static void MethodSyntax()
{
// Query Syntax
var querySyntaxQuery = from item in items
where item.IsActive
select new { item.Name, item.Id };
Console.WriteLine("\nQuery Syntax Results:");
foreach (var item in querySyntaxQuery)
{
Console.WriteLine($"Name: {item.Name}, Id: {item.Id}");
}
}
}
And create a model class as follows
class Item
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
Execute from the main method as follows
#region Day 27: Query vs Method Syntax
static string ExecuteDay27()
{
QueryVsMethod.MethodSyntax();
QueryVsMethod.QuerySyntax();
return "Executed Day 27 successfully..!!";
}
#endregion
Console Output
Query Syntax Results:
Name: Item1, Id: 1
Name: Item3, Id: 3
Method Syntax Results:
Name: Item1, Id: 1
Name: Item3, Id: 3
Complete Code on GitHub
GitHub — ssukhpinder/30DayChallenge.Net
C# Programming🚀
Thank you for being a part of the C# community! Before you leave:
Follow us: Youtube | X | LinkedIn | Dev.to
Visit our other platforms: GitHub
More content at C# Programming