Learn why query syntax is preferred in LINQ. Discover a better approach using Query Syntax on Day 27 of our 30-Day .NET Challenge.
The article demonstrates the use of query and method syntax for writing LINQ queries. In addition to that, it highlights why the query syntax is preferred over method syntax in case of complex queries.
Before diving in, let's understand each syntax for LINQ.
It is one of the syntaxes for writing queries that 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.
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.
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.
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 });
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 };
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; }
}
#region Day 27: Query vs Method Syntax
static string ExecuteDay27()
{
QueryVsMethod.MethodSyntax();
QueryVsMethod.QuerySyntax();
return "Executed Day 27 successfully..!!";
}
#endregion
Query Syntax Results:
Name: Item1, Id: 1
Name: Item3, Id: 3
Method Syntax Results:
Name: Item1, Id: 1
Name: Item3, Id: 3
GitHub — ssukhpinder/30DayChallenge.Net
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