Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

LINQ Concepts - Query syntax

There are two different syntax paradigms for accessing LINQ functionality: query syntax and method syntax.

Query syntax has a very distinctive syntactical style. Due to its unique format, query syntax is very memorable and somewhat iconic as it's often what people envision when thinking of LINQ. Here is an example of LINQ code using query syntax:

Query syntax example

using System.Linq;

   ...

List<string> animalNames = new List<string>
    {"fawn", "gibbon", "heron", "ibex", "jackalope"};
    
// Result: {"heron", "gibbon", "jackalope"}
IEnumerable<string> longAnimalNames =
    from name in animalNames
    where name.Length >= 5
    orderby name.Length
    select name;

For those who are familiar with SQL syntax, you might find some similarities in this example. It's not exactly the same as SQL though. This example gathers all the strings in animalNames that are at least 5 characters in length, ordered from the entry with the fewest characters to the entry with the most characters.

NOTE: The above example includes using System.Linq;. This is essential when using LINQ. Both query syntax and method syntax require it. Although I will not include it in all examples, it is always implied.

Advantages of query syntax

  • The MSDN documentation claims that "many people find query syntax simpler and easier to read."
  • Query syntax can be more compact since the variables created have a scope that spans the whole statement, vs. method syntax where variables need to be re-declared for each method call.

Insane query syntax example

Here is an example of the expressiveness of LINQ query syntax:

Taking LINQ to Objects to Extremes: A fully LINQified RayTracer

Try to decipher that, I challenge you!

Query syntax exercise

Now you try it out. The following code shows a LINQ query that returns the input inValues unmodified. Using the format of the example above, see if you can return only the strings that have pattern in them (using the String.Contains() method) and order the list alphabetically.

Query Syntax Exercise
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content