Writing LINQ in a Nutshell

Adrian Hartanto
Bina Nusantara IT Division
4 min readDec 27, 2021
wallnuts
Photo by Aayush Shah from Unsplash

Definition

LINQ is a tool from C# that can be used for many purposes. One main purpose is to query data from any source database. To use LINQ you will need to add System.Linq namespace. Also, you can use LINQ like creating a method in C#. Therefore you can treat LINQ as the same as a programming language.

Benefits

In my experience using LINQ, benefits from using this type of query to manipulating data is:

  1. Before the query is being compiled, IntelliSense will show an error on your wrong typed query. This helps me a lot to fasten my code typing and also IntelliSense will check my code before I run it.
  2. Can retrieve data from many databases. Because LINQ knows how to convert your code into different queries.
  3. When I’m using LINQ to manipulate the retrieved data. It becomes easier because of syntax methods from LINQ such as Where(), Join(), GroupBy(), and many more.

Three Ways to Write LINQ Query

There are several ways to write a LINQ query. I will show you LINQ by an example.

Here is my list of names.

var listOfName = new List<string>(){"Adrian","Hartanto","Brando","Peter","Raccoon"};

LINQ Query Syntax On a List

So in this writing type, I will try to retrieve the value exact “Adrian”.

var retrieveByQuery = from name in listOfName                      where name == "Adrian"                      select name;

The result from the code above

["Adrian"]

LINQ Method Syntax On a List

Same as is in query syntax. I want to retrieve by method syntax.

var retrieveByMethod = listOfName.Where(name => name == "Adrian")
.Select(name => name);

The result from the code above

["Adrian"]

LINQ Mixed Syntax On a List

Because I want to mix both syntaxes. The result from this query will be a list.

var retrieveByBothSyntax = (from x in listOfName                            where x.Length > 5                            select x).ToList();

The result from the code above

["Adrian","Hartanto","Brando","Raccoon"]

How About Adding a Different Source.

I want to add hobbies for each person in a different source. The parameter to join the two sources is the name.

public class Hobby{public string name { get; set; }public string hobby { get; set; }  public static List<Hobby> GetHobby()  {    return new List<Hobby>()    {      new Hobby { name = "Adrian", hobby = "Swimming" },      new Hobby { name = "Hartanto", hobby = "Programming" },      new Hobby { name = "Brando", hobby = "Studying" },      new Hobby { name = "Peter", hobby = "Running" },      new Hobby { name = "Raccoon", hobby = "Reading" }    };  }}

LINQ Query Syntax On Two Objects

In this line of code, I will only try to join the table.

var nameAndHobbiesByQuery = from name in listOfName  join hobby in Hobby.GetHobby()  on name equals hobby.nameselect new{  name = name,  hisHobby = hobby.hobby};

The result from the code above

[  {    "name": "Adrian",    "hisHobby": "Swimming"  },  {    "name": "Hartanto",    "hisHobby": "Programming"  },  {    "name": "Brando",    "hisHobby": "Studying"  },  {    "name": "Peter",    "hisHobby": "Running"  },  {    "name": "Raccoon",    "hisHobby": "Reading"  }]

LINQ Method Syntax On Two Objects

Same as in query syntax. In this line of code, I will only try to join the table.

var nameAndHobbiesByMethod = listOfName.Join(Hobby.GetHobby(),  name => name,  hobby => hobby.name,  (name, hobby) => new { name, hobby.hobby }).Select(groupedData => new{  name = groupedData.name,  hisHobby = groupedData.hobby}).ToList();

The result from the code above

[  {    "name": "Adrian",    "hisHobby": "Swimming"  },  {    "name": "Hartanto",    "hisHobby": "Programming"  },  {    "name": "Brando",    "hisHobby": "Studying"  },  {    "name": "Peter",    "hisHobby": "Running"  },  {    "name": "Raccoon",    "hisHobby": "Reading"  }]

LINQ Mixed Syntax On Two Objects

In this line of code, I will try to join the table but only retrieve one value.

var nameAndHobbiesBothSyntax = (from name in listOfName  join hobby in Hobby.GetHobby()  on name equals hobby.nameselect new{  name = name,  hisHobby = hobby.hobby}
)
.Where(name => name.name == "Adrian")
.FirstOrDefault();

The result from the code above

{"name": "Adrian","hisHobby": "Swimming"}

Final Thoughts

So these are what I can explain about LINQ. if you already know about LINQ, I recommend you to learn these various syntaxes. Knowing various syntax can help shorten your code and also do complex queries (but not as complex as SQL queries). For performance, any of these syntaxes is the same. So choose your syntax for writing a LINQ query. As for myself, I’m more into the LINQ method syntax.

--

--