Lets take some dummy classes which will help us to understand the linq keywords with example ..
public class Product
{
public int ID { set; get; }
public string ProductName { set; get; }
public double ProductCost { set; get; }
}
public class Employee
{
public int ID { set; get; }
public string EmployeeName { set; get; }
public double Age { set; get; }
}
Product and EMployee list :
List<Product> productList = new List<Product>()
{
new Product {ID=10,ProductName=”Apidra”,ProductCost=20.20},
new Product {ID=11,ProductName=”Lantus”,ProductCost=30.20},
new Product {ID=12,ProductName=”Sinarest”,ProductCost=20.20},
new Product {ID=13,ProductName=”Nice”,ProductCost=20.20},
new Product {ID=14,ProductName=”Disprin”,ProductCost=30.20}
};
List< Employee >employeeList = new List< Employee>()
{
new Employee {ID=10,EmployeeName=”Scott”,Age=35},
new Employee {ID=11,EmployeeName=”Davis”,Age=27},
new Employee {ID=12,EmployeeName=”Anis”,Age=32},
new Employee {ID=13,EmployeeName=”Tauseef”,Age=35},
new Employee {ID=14,EmployeeName=”Neeju”,Age=31}
};
1. FirstOrDefault() :
Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
Summary:
Type Parameters:
TSource: The type of the elements of source.
Parameters:
source: The System.Collections.Generic.IEnumerable to return the first element of.
Returns:
default(TSource) if source is empty; otherwise, the first element in source.
Exceptions:
System.ArgumentNullException: source is null.
Example :
var res = productList.FirstOrDefault(x => x.ProductCost == 30.20);
if (res != null)
{
Console.WriteLine(string.Format(“ID={0} , Product Name = {1} , Product Cost = {2}”, res.ID, res.ProductName, res.ProductCost));
}
Result :
If condition mate :
ID=11 , Product Name = Lantus , Product Cost = 30.2
If not then : NULL
2.First() :
Type Parameters: TSource: The type of the elements of source.
Parameters: N/A
source: An System.Collections.Generic.IEnumerable to return an element from.
predicate: A function to test each element for a condition.
Returns: The first element in the sequence that passes the test in the specified predicate function.
Exceptions:
System.ArgumentNullException: source or predicate is null.
System.InvalidOperationException: No element satisfies the condition in predicate.-or-The source sequence is empty.
var res = productList.First(x => x.ProductCost == 30.20);
if (res != null)
{
Console.WriteLine(string.Format(“ID={0} , Product Name = {1} , Product Cost = {2}”, res.ID, res.ProductName, res.ProductCost));
}
Result :
If condition mate :
ID=11 , Product Name = Lantus , Product Cost = 30.2
If not then : System.ArgumentNullException
3. Single() :
Summary:
Type Parameters: NA
TSource: The type of the elements of source.
Parameters:NA
source: An System.Collections.Generic.IEnumerable to return the single element of.
Returns: Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
Exceptions:
System.ArgumentNullException: source is null.
System.InvalidOperationException: The input sequence contains more than one element.-or-The input sequence is empty.
res = productList.Single(x => x.ProductCost == 10.20);
if (res != null)
{
Console.WriteLine(string.Format( Product Name = {0} , Product Cost = {1}”, res.ProductName, res.ProductCost));
}
If No record found : error : Sequence contains no matching element
If more than one record found : error: Sequence contains more than one matching element
If matches : Product Name = Lantus , Product Cost = 10.20
4 .SignleOrDefault() :
Summary:
TSource: The type of the elements of source.
Parameters: NA
source: An System.Collections.Generic.IEnumerable to return a single element from.
predicate: A function to test an element for a condition.
Returns: Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition.
Exceptions:
System.ArgumentNullException: source or predicate is null.
Example :
res = productList.Single(x => x.ProductCost == 10.20);
if (res != null)
{
Console.WriteLine(string.Format( Product Name = {0} , Product Cost = {1}”, res.ProductName, res.ProductCost));
}
If No record found : NULL
If more than one record found : error: Sequence contains more than one matching element
If matches : Product Name = Lantus , Product Cost = 10.20
5. FindAll()
Summary: Retrieves all the elements that match the conditions defined by the specified predicate.
Parameters: The System.Predicate delegate that defines the conditions of the elements to search for.
Returns:
A System.Collections.Generic.List containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty System.Collections.Generic.List.
Exceptions:
System.ArgumentNullException: match is null.
Example:
var result = productList.FindAll(x => x.ProductCost == 20.20);
foreach (var resdetail in result)
{
Console.WriteLine(string.Format(“Product Name = {0} , Product Cost = {1}”, resdetail.ProductName, resdetail.ProductCost));
}
Output:
ID=10 , Product Name = Apidra , Product Cost = 20.2
ID=12 , Product Name = Sinarest , Product Cost = 20.2
ID=13 , Product Name = Nice , Product Cost = 20.2
If No record found then : count=0
Summary :
In this post we discussed few popular keywords in LINQ. Will discuss on other few keywords in upcoming posts. Please provide your valuable comments and suggestions to improve more.