You are here to dive deeper into the LINQ world. LINQ, Language Integrated Query, allows you to search, filter, and manipulate data sources elegantly and efficiently in C#. In this guide, we will look at how you can work with classes and lists in combination with LINQ to perform targeted queries on your data.
Key Insights
- LINQ simplifies data querying through a clearly structured syntax.
- You can efficiently use classes and lists to organize and query data.
- The use of LINQ allows for quick searching and filtering of large datasets.
Step-by-Step Guide
To learn the basics of LINQ, we will first create a class for our postal codes.

Start with the necessary using directives to add the required namespaces. You will need System.Collections.Generic for collections and System.Linq for LINQ queries.
Creating a class is straightforward. As an example, I have already prepared a postal code class. This class has properties for the location, district, and postal code.
Next, you should create a list to store multiple instances of the postal code class. Here, I use a list called postalCode. This is done with the statement new List
In this list, I store various entries, for example: Berlin – Neukölln with the corresponding postal code. I have included a total of six postal codes with their associated locations and districts. Here’s an example: Location Berlin, District Neukölln, Postal Code 12043.
For the queries, we use the IEnumerable interface. First, we declare the data type. We simplify the process with the keyword var.
Now we can create a LINQ query to get all postal codes from Hannover. For this, use from postalCode in postalCode and filter the list with the condition where postalCode.location == "Hannover".

The query result is stored in the variable PLZ_Hannover. Note that we have only set up the query here; it has not yet been executed.
To execute the query, use a foreach loop. We print each selected postal code to the console to display the results. When you start the program, you should only see the postal codes from Hannover.
Here’s an exercise for you: Print the district of the postal code 30559 to the console. Use the LINQ query according to the previous steps and ensure you use the correct filter syntax.
Here is a possible solution. You can come up with an example like this: var postalCode_district = from district in postalCode where district.PostalCode == 30559 select district.District.
If you executed this correctly, you should see "Kirchrode" on the console. Check the list to ensure that this is the correct output.

Using LINQ, it is really easy to query data once you understand the basic syntax. One of the strengths of LINQ is that large datasets can be filtered and sorted quickly.
In the next video, we will provide another practical example. Look up this small example and play around with it. Design your own queries and continue practicing – this will help you become a true LINQ professional.
Summary – Using LINQ in C#: Classes and Lists in Detail
In this guide, you learned how to effectively use LINQ with classes and lists in C#. The concepts have been clearly outlined and allow you to create your own queries and process data efficiently.
Frequently Asked Questions
How do I create a list in C#?You can create a list in C# using new List(), where Type is the data type that the list should store.
What is the benefit of using LINQ?LINQ allows precise and understandable data queries within C#, leading to greater efficiency and readability.
How can I execute a LINQ query?To execute a LINQ query, you use a foreach loop to iterate over the results and display them.