Linq
Chaining Query Operators
private static void Main()
{
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
IEnumerable<string> query = names
.Where(n => n.Contains("a"))//Filter
.OrderBy(n => n.Length)//Sort
.Select(n => n.ToUpper());//Convert
foreach (string name in query)
{
Console.WriteLine(name);
}
Console.ReadLine();
}
Finding Specific items from list
int[] numbers = { 10, 9, 8, 7, 6 };
int firstNumber = numbers.First(); // 10
int lastNumber = numbers.Last(); // 6
int secondNumber = numbers.ElementAt(1); // 9
int secondLowest = numbers.OrderBy(n=>n).Skip(1).First(); // 7
Where method
- The
Where()method is part of the LINQ extension methods available in C#. It allows you to filter elements from a collection or sequence based on a specified condition, and returns a new collection or sequence that contains only the elements that satisfy the condition. - Here's the basic syntax of the
Where()method: - where
collectionis the sequence or collection that you want to filter,elementis a placeholder for each individual element in the collection, andconditionis the predicate that determines whether or not an element should be included in the result. - In this example, the lambda expression
num => num % 2 == 0is the condition that checks whether a number is even or not. TheWhere()method returns a newIEnumerable<int>that contains only the even numbers from the original list, which are2,4, and6.
Find method
- The Find() method is part of the LINQ extension methods available in C#. It allows you to retrieve the first element in a sequence or collection that satisfies a specified condition. Here's the basic syntax of the Find() method:
- In this example, the lambda expression
name => name.StartsWith("A")is the condition that checks whether a name starts with the letter "A". TheFind()method returns the first element in thenameslist that satisfies the condition, which is the string "Alice". - Note that the
Find()method throws an exception if no element in the collection satisfies the condition. If you want to handle this case, you can use theFirstOrDefault()method instead, which returns the default value (usually null) if no element is found.
FindAll method
- The
FindAll()method is part of the LINQ extension methods available in C#. It allows you to retrieve all elements in a sequence or collection that satisfy a specified condition, and returns a new collection that contains only those elements. - Here's the basic syntax of the
FindAll()method:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
var result = numbers.FindAll(num => num % 2 == 0);
num => num % 2 == 0 is the condition that checks whether a number is even or not. The FindAll() method returns a new List<int> that contains only the even numbers from the original list, which are 2, 4, and 6.
Difference between FindAll and Where Method
- Return Type: Where() method returns IEnumerable
while FindAll() returns a List - Method Chaining:
Where()can be chained with other methods likeSelect()andOrderBy()etc, whileFindAll()does not support chaining with other LINQ methods.
### Linq Cast - In C#, the Cast method is used to convert an IEnumerable collection to a new collection of a specified type. It is part of the System.Linq namespace and is used in LINQ queries to transform one collection into another. - The Cast method works by iterating through each element in the source collection and attempting to cast it to the specified type. If an element cannot be cast to the desired type, an InvalidCastException will be thrown. - Here's an example of how to use the Cast method to convert a collection of objects to a collection of integers: ```csharp using System; using System.Collections.Generic; using System.Linq;
class Program { static void Main(string[] args) { List
IEnumerable<int> integers = objects.Cast<int>();
foreach (int i in integers)
{
Console.WriteLine(i);
}
}
} ```