In this article we are going to see the frequently used extension methods in LINQ with the help of Lambda expressions.
Used Namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
Used collection for Demo
public static List<int> GetNumberList()
{
List<int> list = new List<int>();
list.Add(11);
list.Add(22);
list.Add(77);
list.Add(13);
list.Add(17);
list.Add(41);
return list;
}
ALL () LINQ Method
It is used to check whether the entire item in the list matches the specified condition. If one item fails, it returns false.
/// <summary>
/// Just to check whether all element satisfies the given condition or not.
/// Return just TRUE or FALSE.
/// It applies to all the DataType like string,datetime etc and also for custom class.
/// </summary>
public static void LinqIntALL()
{
List<int> list = GetNumberList();
//It returns bool, check whether all the item in the list greather than 13.
bool isAll = list.All(i => i > 13)
//Print
HttpContext.Current.Response.Write("all greather than 13 :" + isAll + "<br/>");
//It returns bool, check whether all the item in the list greather than 10.
isAll = list.All(i => i > 10);
HttpContext.Current.Response.Write("all greather than 10 :" + isAll + "<br/>");
}
ANY () LINQ Method
It is used to check whether any item satisfies the given condition. If one item satisfies then it returns true otherwise false.
/// <summary>
/// Just to check whether all element satisfies the given condition or not.
/// Return just TRUE or FALSE.
/// It applies to all the DataType like string,datetime etc and also for custom class.
/// </summary>
public static void LinqIntANY()
{
List<int> list = GetNumberList();
//It returns bool, Just to check any element is greater than 13.
bool isAll = list.Any(i => i > 13);
//Print
HttpContext.Current.Response.Write("Any greather than 13 :" + isAll + "<br/>");
//It returns bool, Just to check any element is equal to 11.
isAll = list.All(i => i == 10);
HttpContext.Current.Response.Write("Any equal to 11 :" + isAll + "<br/>");
}
TAKE () LINQ Method
Used to get the Top "N" items from the list. Note it can be taken in sequence order.
/// <summary>
/// This is used to take Top 'N' items form the list.Like "Top 5" in sql.
/// </summary>
public static void LinqInTake()
{
List<int> list = GetNumberList();
//This takes only the Top two items from the list and eliminate others.
//"2" in param indicates how much item you need.
//ToList() helps to convert IEnumerable to List.
list = list.Take(2).ToList();
//Print
PrintLis<int>(list, "Take Only top two item");
}
TAKEWHILE () LINQ Method
Used to take items from a list based on the given condition. It iterates sequentially and checks each and every item, if a condition
is satisfied then it adds to a collection to the specific count.
/// <summary>
/// This is used to take Top 'N' items based on the given condition.
/// </summary>
public static void LinqInTakeWhile()
{
List<int> list = GetNumberList();
//This takes item if the element is less than 77 and eliminate others.
//ToList() helps to convert IEnumerable to List.
list = list.TakeWhile(i => i < 77).ToList();
//Print
PrintLis<int>(list, "Take items less than 77");
//This takes item if the element is less than 22 and eliminate others.
//ToList() helps to convert IEnumerable to List.
list = list.TakeWhile(i => i < 22).ToList();
//Print
PrintLis<int>(list, "Take items less than 22");
}
SKIP () LINQ Method
Used to skip the top items based on the given count.
/// <summary>
/// This is used to Skip Top 'N' items form the list.
/// </summary>
public static void LinqInSKIP()
{
List<int> list = GetNumberList();
//This SKIPS the Top two items from the list and keep remaining.
//"2" in param indicates how much item you need to skip.
//ToList() helps to convert IEnumerable to List.
list = list.Skip(2).ToList();
//Print
PrintLis<int>(list, "Skips top two item");
}
SKIPWHILE () LINQ Method
Used to skip items from the list based on the given condition.
It checks each and every item sequentially and if a condition is true then it skips the items up to where the condition turns false.
/// <summary
/// This is used to Skip Top 'N' items form the list based on the given conditon.
///</summary>
public static void LinqInSKIPWhile()
{
List<int> list = GetNumberList();
//This SKIPS upo the item equals to 11.
//Sequence check if any item not equal to 11, it return default list.
//ToList() helps to convert IEnumerable to List.
list = list.SkipWhile(i => i == 11).ToList();
//Print
PrintLis<int>(list, "Skips the item upto it equals to 11");
}