Saturday, May 1, 2010

LINQ Samples (Restriction operator) : Select simple 1

A simple where clause used to find all elements that are greater than value 'x'

private void LINQWhereOne()
{
//Find Array where elements are less than x;
int x = 25;

int[] numeric = new int[] { 12, 18, 35, 50, 24, 25, 10, 15, 18 };
var lessThanX = numeric.Where(a => a < x);

Console.WriteLine("Array where elements are less than {0}",x);
foreach(int i in lessThanX )
{
Console.WriteLine("{0}",i);
}
}

OUTPUT :

Array where elements are less than 25
12
18
24
10
15
18


LINQ Samples (Restriction Operators) : Select simple 2
Going forward with 'Where' another simple example that works with Object

private void LINQWhereTwo()
{
//Find product where stock is 0
var noStock = prod.Where(a => a.Stock <= 0);

Console.WriteLine("Product where stock is 0");
foreach (Product pd in noStock)
{
Console.WriteLine("ProductId : {0} ,ProductName : {1} , ProductPrice : {2} , Stock : {3}",
pd.ProductId, pd.ProductName, pd.ProductPrice, pd.Stock );
}
}

OUTPUT :

Product where stock is 0
ProductId : 5 ,ProductName : Noodles , ProductPrice : 25.4 , Stock : 0
ProductId : 6 ,ProductName : Washing Powder , ProductPrice : 25.4 , Stock : 0

No comments:

Post a Comment