Day 27 of 30-Day .NET Challenge: Query v/s Method Syntax

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

Prerequisites for Developers

30 Day .Net Challenge

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 });
Enter fullscreen mode
Exit fullscreen mode

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 };
Enter fullscreen mode
Exit fullscreen mode

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 });
Enter fullscreen mode
Exit fullscreen mode

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 };
Enter fullscreen mode
Exit fullscreen mode

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}"); } } }
Enter fullscreen mode
Exit fullscreen mode

And create a model class as follows

 class Item { public int Id { get; set; } public string Name { get; set; } public bool IsActive { get; set; } }
Enter fullscreen mode
Exit fullscreen mode

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
Enter fullscreen mode
Exit fullscreen mode

Console Output

 Query Syntax Results: Name: Item1, Id: 1 Name: Item3, Id: 3 Method Syntax Results: Name: Item1, Id: 1 Name: Item3, Id: 3
Enter fullscreen mode
Exit fullscreen mode

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


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Please set api key. This full body massager provides the best kneading and shiatsu massages that give the feel of real human hand massaging.