If you are dealing with programming in C#, it is important to understand the basics of LINQ (Language Integrated Query). LINQ allows you to perform queries on arrays, lists, and other collections. In this tutorial, you will learn how to create an array of postal codes and query it efficiently using LINQ. We will go through the entire process step by step, so that by the end, you will be able to create your own queries.
Key takeaways
- LINQ makes traversing and querying collections easier.
- To use LINQ, specific namespaces need to be imported.
- Queries are created by using variables and LINQ methods.
Step-by-step Guide
Step 1: Creating the Array
To begin with, you create an array of integer values that represent postal codes in your case. In this example, we have a small selection of postal codes from German cities like Berlin, Hamburg, and Hannover.

This forms our data source, which we will later query with LINQ.
Step 2: Adding Namespaces
To use LINQ in your C# project, you need to import two important namespaces. This is done by the using directive at the beginning of your code. You need System.Collections.Generic and System.Linq.

Now you are ready to create active LINQ queries.
Step 3: Creating a LINQ Query
Now you need a query to select the postal codes from your array. You can use LINQ syntax to retrieve all postal codes at once.

In this case, each postal code from the array is stored in the variable plzAbfrage. Care must be taken to follow the syntax exactly.
Step 4: Executing the Query
Now that you have created the query, the next step is to actually execute it and output the results. This is done by iterating through plzAbfrage with a foreach loop.
When you start your program now, you should see the postal codes exactly as they were in the array in the console.
Step 5: Analyzing the Output
However, the postal codes may appear in random order. Therefore, it might be interesting to perform an ascending or descending sort, which we will look at in more detail in a future video.

With the right LINQ syntax, you can easily sort results based on specific criteria.
Summary – LINQ and Array Queries in C
In this tutorial, you have learned the basics of LINQ and working with arrays in C#. You created an array, added namespaces, defined a query, and executed it successfully. You are now ready to dive deeper into the subject and formulate more complex queries.
Frequently Asked Questions
What is LINQ?LINQ stands for Language Integrated Query and allows queries on collections in C#.
How do I create an array in C#?You can define an array using the keyword int[] followed by the values in curly braces.
What namespaces are needed to use LINQ?You need to import the namespaces System.Collections.Generic and System.Linq.
How do I output values from a LINQ query?The best way is with a foreach loop that iterates over the query result.
Why do my outputs appear in random order?The output follows the order of the data in the array. Sorting can be done using LINQ.