![]() |
VOOZH | about |
LINQ (Language Integrated Query) provides two ways to write queries in C#
LINQ Method Syntax, also called Fluent Syntax, uses extension methods such as Where(), Select(), OrderBy() and others, defined in the System.Linq namespace. It provides the same functionality as Query Syntax but often offers more flexibility, especially for complex queries.
Instead of SQL-like keywords, Method Syntax uses lambda expressions as parameters to LINQ methods.
Step 1: First add System.Linq namespace in your code.
using System.Linq;
Step 2: Next, create a data source on which you want to perform operations.
For example:
List<string> my_list = new List<string>() {
"This is my Dog",
"Name of my Dog is Robin",
"This is my Cat",
"Name of the cat is Mewmew"
};
Step 3: Create the query using LINQ methods like Where(), Select(), etc.
For example:
var res = my_list.Where(l => l.Contains("my")).Select(l => l);
Here:
Step 4: Execute the query by using a foreach loop.
For example:
foreach (var q in res) {
Console.WriteLine(q);
}
Example: Program to demonstrate LINQ Method Syntax
This is my Dog Name of my Dog is Robin This is my Cat