Appendix L. LINQ
This appendix provides syntax summaries for the most useful LINQ methods. For more detailed information, see Chapter 21, "LINQ."
Basic LINQ Query Syntax
The following text shows the typical syntax for a LINQ query:
From ... Where ... Order By ... Select ...
The following sections describe these four basic clauses. The sections after those describe some of the other most useful LINQ clauses.
From
The From clause tells where the data comes from and defines the name by which it is known within the LINQ query.
Fromvar1Indata_source1,var2Indata_source2, ...
Examples:
Dim query1 = From cust As Customer In all_customers Dim query2 = From stu In students, score In TestScores
Usually, if you select data from multiple sources, you will want to use a Where clause to join the results from the sources.
Where
The Where clause applies filters to the records selected by the From clause. The syntax is:
Where conditionsUse comparison operators (>, <, =), logical operators (Not, Or, AndAlso), object methods (ToString, Length), and functions to build complex conditions.
For example, the following query selects student and test score data, matching students to their test scores:
Dim query = From stu In students, score In TestScores _
Where stu.StudentId = score.StudentIdThe following example selects only students with last names starting with S:
Dim query = From stu In students, score In TestScores _
Where stu.StudentId = score.StudentId AndAlso _
stu.LastName.ToUpper.StartsWith("S")Order By ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access