Monday, May 3, 2010

LINQ Samples (Partitioning operator) : 'Take' simple and Nested

Example simple : Example takes the first 5 values from the collection using the 'Take' operator


public void TakeSimple()
{
int[] num = { 34,56,72,74,223,442,4, 61 , 40 };

Console.WriteLine("Print num ");
foreach (int j in num)
Console.Write("{0} ",j);

Console.WriteLine("\n\nPrint num with take(5) ");
IEnumerable i = num.Take(5);

foreach (int j in i)
{
Console.Write("{0} ",j);
}
}


OUTPUT :

Print num
34 56 72 74 223 442 4 61 40

Print num with take(5)
34 56 72 74 223



Example Nested : Following example first check a condition using restriction operator 'where', then applies 'Take' to the result


public void TakeNested()
{
int[] num = { 34, 36 , 56, 72, 45 , 74, 223, 442, 4, 61, 40 };

Console.WriteLine("Print num");
foreach (int j in num)
Console.Write("{0} ", j);

Console.WriteLine("\n\nPrint num where value > 25");
IEnumerable i1 = num.Where(a => a > 25);
foreach (int j in i1)
Console.Write("{0} ", j);

Console.WriteLine("\n\nPrint num where value > 25 and take only 5");
IEnumerable i2 = num.Where(a => a > 25).Take(5);
foreach (int j in i2)
Console.Write("{0} ", j);
}


OUTPUT :

Print num
34 36 56 72 45 74 223 442 4 61 40

Print num where value > 25
34 36 56 72 45 74 223 442 61 40

Print num where value > 25 and take only 5
34 36 56 72 45

LINQ Samples (Projection Operators) : Select Compound 2

Following example uses LINQ lambda on a 2 set of objects. Eventhough join is used here the condition is set to 'true', simply to join the collection.

Also note the 'query' equivalent of the lambda code as commented out section .(Eventhough the query expression is easier to write, this examples are all in lambda expression of people who are interested in lambdas)


private void SelectManyCompound()
{
List prodA = new List {
new Product { productID=1, productName="Prod1", productPrice=100.50, stockQuantity= 10 },
new Product { productID=2, productName="Prod2", productPrice=89.50, stockQuantity= 15 },
new Product { productID=3, productName="Prod3", productPrice=10.00, stockQuantity= 120 },
new Product { productID=4, productName="Prod4", productPrice=19.50, stockQuantity= 30 },

};

List orderA = new List {

new Order { CustomerName="Ben", OrderNo=1, TotalAmt= 1000},
new Order { CustomerName="Alice", OrderNo=2, TotalAmt= 900},
new Order { CustomerName="Sam", OrderNo=3, TotalAmt= 700},
};

var v = prodA.Join(orderA, a => true, b => true,
(a, b) =>
new
{
Prod = a,
order = b
})
.Where( (c) => c.Prod.productPrice > 75 );

/* Equivalent code using Query
var v = from a in prodA
from b in orderA
where a.productPrice > 75
select new { Prod = a, order = b };
*/

foreach (var v1 in v)
{
Console.WriteLine("ProdId:{0}, ProdName:{1}, Price:{2}, Qty:{3}, CustNm:{4}, OrdNo:{5},Total:{6}",
v1.Prod.productID,v1.Prod.productName,v1.Prod.productPrice,v1.Prod.stockQuantity,
v1.order.CustomerName,v1.order.OrderNo,v1.order.TotalAmt
);
}
}


OUTPUT

ProdId:1, ProdName:Prod1, Price:100.5, Qty:10, CustNm:Ben, OrdNo:1,Total:1000
ProdId:1, ProdName:Prod1, Price:100.5, Qty:10, CustNm:Alice, OrdNo:2,Total:900
ProdId:1, ProdName:Prod1, Price:100.5, Qty:10, CustNm:Sam, OrdNo:3,Total:700
ProdId:2, ProdName:Prod2, Price:89.5, Qty:15, CustNm:Ben, OrdNo:1,Total:1000
ProdId:2, ProdName:Prod2, Price:89.5, Qty:15, CustNm:Alice, OrdNo:2,Total:900
ProdId:2, ProdName:Prod2, Price:89.5, Qty:15, CustNm:Sam, OrdNo:3,Total:700

LINQ Samples (Projection Operators) : Select Compound

Following example is using select on 2 set of collections.The example is equivalent to the query

var v = from a in numA
from b in numB
where a < b
select new { a = a,b = b};


private void SelectMany()
{
List numA = new List { 12, 25, 5, 6, 7, 28, 9 };
List numB = new List { 11, 22, 15, 16, 27 };

var v = numA.Join(numB, a => true, b => true,
(a, b) => new { a= a, b=b }).Where(c=>c.a < c.b );

foreach (var v1 in v)
{
Console.WriteLine("{0}",v1 );
}
}


OUTPUT

{ a = 12, b = 22 }
{ a = 12, b = 15 }
{ a = 12, b = 16 }
{ a = 12, b = 27 }
{ a = 25, b = 27 }
{ a = 5, b = 11 }
{ a = 5, b = 22 }
{ a = 5, b = 15 }
{ a = 5, b = 16 }
{ a = 5, b = 27 }
{ a = 6, b = 11 }
{ a = 6, b = 22 }
{ a = 6, b = 15 }
{ a = 6, b = 16 }
{ a = 6, b = 27 }
{ a = 7, b = 11 }
{ a = 7, b = 22 }
{ a = 7, b = 15 }
{ a = 7, b = 16 }
{ a = 7, b = 27 }