Writing Queries In CSharp

Опубликовано: 24 Июль 2026
на канале: DotNetAcademy
68
0

Video How to: Creating Queries in C# (LINQ)
Transcript
I'm Harry Miller, and in this presentation, I'm going to show you how to write queries in Visual C#.
Using Language-Integrated Query (LINQ), you can query data from any data source that supports LINQ, such as a SQL Server database, XML, in-memory arrays and collections, and ADO.NET datasets. Because the queries are written in the C# language, your query results are returned as strongly-typed objects.
I'll demonstrate how to create queries on a list of data objects that contain the attributes of a set of fictional students. The queries incorporate several new features of Visual C#, including auto-implemented properties, object initializers, and collection initializers.
Create a Project
I'll start by creating a Visual C# Console Application. I'll click the File menu, go to New, Project, and then click Console Application.
LINQ query operations consist of three actions:
1. Obtain the data source or sources.
2. Create the query.
3. Execute the query.
Create an In-Memory Data Source
I'm going to use a list of Student objects as my data source. Each Student object contains a first name, a last name, an ID, and a list of that student's test scores.
First I'll add the code that defines the Student class and the Student data to my project, in the Program class.
The Student class consists of auto-implemented properties.
The list of Student objects that is the data source is created using a collection initializer.
Following the pattern of the code here, I'll add another instance of the Student class to the list. Using this syntax, called object initializers, I can declare my object and initialize its values in one line of code.
This whole data structure will be initialized and instantiated without explicit calls to any constructor or explicit member access.
Create a Query
Now I'll create a simple query to produce a list of the students whose score on the first test was greater than 90.
I'll paste my code in the Main method.
Notice that this query is very much like a SQL query – there's a From statement, Where statement, and Select statement. But I'm able to write this query language directly in my C# code.
And if I type the query manually, I get IntelliSense as well.
Run the Query
Keep in mind that the variable studentQuery only contains the definition of the query – not the results of running the query. The typical mechanism for running a query is to use a For Each loop.
So I'll add a For Each loop to iterate through all the student objects that are returned by the query, and write the names out to the console. I'll also add Console.ReadLine() so the console window stays open after the code runs.
Now I'll run the application, and the console window displays the results. The results are listed in the order they appear in the data source.
Modify the Query
Now I'll modify the query to show the results differently. I'll add an orderby clause between the Where statement and the Select statement of the query. This will order the results alphabetically based on the last name. I'll specify ascending order.
I'll run the project, and the list is now ordered alphabetically.
You can use many more query operators to make your queries specific. For examples of more complex queries, see the topic "Walkthrough: Writing Queries in C#."
For More Information
You can get more information about developing Visual C# applications in the Visual C# Help. You can find other resources such as technical articles, samples, blogs, and videos at the Visual C# Developer Center