LINQ is short for Language-Integrated Query, which is basically a set of technologies. The idea from Microsoft was to integrate query capabilities directly into the C# language. For these types of queries, you need to know different query languages to work with SQL databases, XML, or various web services, unlike with traditional queries which are expressed as simple strings and interpreted by the engine.
In this guide, we will first clarify what LINQ is with an example, then we'll demonstrate the difference between query and method syntax.
Let's create a small demonstration that uses LINQ to filter lottery numbers higher than a specified value.
1using System;
2using System.Linq;
3using System.Collections.Generic;
4
5namespace Pluralsight
6{
7 public class Linqing
8 {
9
10 public static void Main()
11 {
12 int[] lottery = new int[] { 11,22,33,44,55,66};
13
14 IEnumerable<int> lotteryQuery =
15 from lot in lottery
16 where lot > 20
17 select lot;
18
19 foreach (int i in lotteryQuery)
20 {
21 Console.Write(i + " ");
22 }
23 Console.ReadKey();
24 }
25 }
26}
The output is as follows.
122 33 44 55 66
At the heart here lies the LINQ query, which is very similar to the SQL
queries most of us love to write. The where
clause defines the filter above that we are interested in. Then foreach
allows us to grab the values caught in the filter.
1from lot in lottery
2where lot > 20
3select lot;
Query and method syntax are semantically identical, but most developers, including me, find the query syntax easier to read and troubleshoot. The reference documentation for the query operators can be found here. We could say that these two approaches are just different tools for the same job. Behind the scenes, the compiler always converts the query syntax to method syntax as it needs function calls.
We can convert our query syntax to method syntax this way.
1IEnumerable<int> lotteryQuery = lottery.Where(num => num > 20);
Iterating over the results in the for loop produces the same results. If we were to check the type of the output variable, it would be revealed that it is an instance of the IEnumerable<T>
class.
The general rule of thumb for either method or query syntax is:
With method syntax we have functions such as:
The let
keyword is a pretty amazing feature of query-based syntax. Simply, it lets you store the results for later use in the query. Let's look at a demonstration. Imagine that we have a server park that includes the server's name, type, and uptime. We want to query the servers with more than 2 years of uptime.
In query syntax, it would look like this.
1var MS =
2 from server in servers
3 let age = GetUptime(server)
4 where age > 2
5 orderby age
6 select server.Name;
In method syntax, we could do something like this.
1var QS = servers
2 .Select(server => new {
3 age = GetYearsWorking(server), Name = server.Name })
4 .Where(x => x.age > 4)
5 .OrderBy(x => x.age)
6 .Select(x => x.Name);
In this case, the method syntax makes it harder to unwrap what the intention was, and in case of troubleshooting it would not be ideal.
There might be a situation where you have two different datasources and you would like to project each element of a sequence to an IEnumerable<T>
.
1var ids = Enumerable.Range(1, 10); //1,2,3
2var names = new string[] { "DC", "DNS", "DHCP", "APP", "ISM", "FND", "SQL", ""};
3
4var QS = from id in ids
5 from name in names
6 select $"map [{id}, {name}]";
7
8var MS =
9 rows.SelectMany(id => names, (i, n) => $"cell [{i}, {n}]");
Iterating over the datastructure, the following output would be shown.
1cell [1, DC]
2cell [1, DNS]
3cell [1, DHCP]
4cell [1, APP]
5cell [1, ISM]
6cell [1, FND]
7cell [1, SQL]
8cell [1, ]
9cell [2, DC]
10...
The query syntax is easy to follow, while the method syntax introduces head-scratching into the equation.
There is a fun github repository that has a tool for converting between these two syntaxes, which you can check out here.
In this guide, we have seen what query and method syntax approaches are in LINQ. We also learned that no matter which we use, the query syntax is converted to method syntax behind the scenes. I hope this guide has been informative to you and I would like to thank you for reading it!