Different ways to write LINQ query

Different ways to write LINQ query

29 Mar 2024
Beginner
16.6K Views
2 min read
Learn via Video Course & by Doing Hands-on Labs

ASP.NET MVC with WebAPI Course

LINQ provides a uniform programming model (i.e. common query syntax) to query data sources (like SQL databases, XML documents, ADO.NET Datasets, Various Web services and any other objects such as Collections, Generics, etc.). LINQ provides you three different ways to write a LINQ query in C# or VB.

Query Expression (Query Syntax)

Query expression syntax is like as SQL query syntax with just a few minor deviations. The result of a query expression is a query object, which is usually a collection of type IEnumerable<T> or IQueryable<T>. This syntax is easy to read and write and at compile time, query expression is converted into Method Invocation.

Query Expression Syntax

from [identifier] 
in [source collection]
let [expression]
where [boolean expression]
order by [expression(ascending/descending)]
select [expression]
group [expression] by [expression] 
into [expression]

Query Expression Example

// Datasource
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Query based syntax
IEnumerable query =
 from num in numbers
 where num > 5 && num < 10
 select num;
//result: 6,7,8,9

Method Invocation (Method Syntax)

Method syntax is complex as compared to Query expression since it uses lambda expression to write LINQ query. It is easily understood by .NET CLR. Hence at compile time, Query expression is converted into Method Invocation. The result of a Method syntax is also a query object, which is usually a collection of type IEnumerable<T> or IQueryable<T>.

[source collection]
.Where [boolean expression]
.OrderBy [expression(ascending/descending)]
.Select [expression]
.GroupBy [expression]

Method Syntax Example

// Datasource
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Method based syntax
IEnumerable<int> query =numbers.Where(num => num > 5 && num < 10)
//result: 6,7,8,9

Note

There are no semantic differences (in terms of performance, execution) between Query Syntax and Method Syntax.

Mixed Syntax

You can use a mixture of both syntaxes by enclosing a query expression inside parentheses and make a call to a method.

Mixed Syntax Example

// Datasource
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Mixed syntax
int query = (from num in numbers
 where num > 5 && num < 10
 select num).Count();
//result: 4
What do you think?

I hope you will enjoy LINQ query syntax while programming with LINQ. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
ASP.NET Core Certification Training May 26 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training May 26 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
ASP.NET Core Certification Training Jun 21 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this