An example of how to use LINQ in C#
The following example shows how to run a LINQ query.
Code:
using System;
using System.Linq;
namespace ConsoleApplications
{
class TestLinqInCSharp
{
static void Main(string[] args)
{
string[] customers = { "Arianne",
"Bill",
"David",
"Manuel",
"Veronic",
"Asia",
"Daniel",
"Renate",
"Vincent",
"Alexia",
"Daisy"};
var nameQuery =
from name in customers
where (name.StartsWith("A"))
select name;
foreach (string n in nameQuery)
Console.WriteLine("{0}", n);
}
}
}
Result:
Arianne
Asia
Alexia
In this example there are three basic parts to every LINQ query:
1. Get the data source.
2. Create the query.
3. Execute the query.