Note: These LINQ sample codes are written keeping the beginner in mind so the code is made as simple as possible.
LINQ Samples series intro: LINQ-Language INtegrated Query is a great .Net language feature, to simple put, it allows you to query data objects in the way you query database tables using english sentence like syntax.
string[] strArray = new string[] { "apple", "orange", "grape", "lemon" };
IEnumerable s = from x in strArray
where x.Contains("ap")
select x;
Console.WriteLine(s);
Output:
apple
grape
As this is the first LINQ sample I am explaining a little bit about how the code executes internally.
Here “x” transforms itself as an item in the strArray and the LINQ code runs for each element in strArray, the “where” section is used to specify the condition, which here uses the x(an element of the array) and “select” section is where the final output of the LINQ query is returned, here it is the array elements.