Monday, April 26, 2010

LINQ Samples (Projection Operators) : Select Indexed

In this example we explore the index for a given enumeration. In the statement
var str = strTech.Select( (a, b) => new { condition = a.Length > b , strTech = a });


a is the object strTech , b in the index or position of enumeration, new {} is the anonymous type.


private void SelectIndexed()
{
int[] num = { 1,2,3,4,5,6,7,8,9};
string[] strTech = new string[] { "C#", "asp.net", "vb.net", "j#", "f#", "Cobol.net", "LINQ", "WCF", "WPF", "Silverlight" };

var str = strTech.Select( (a, b) => new { condition = a.Length > b , strTech = a });

foreach (var s in str)
{
Console.WriteLine("{0}\t{1}", s.strTech, s.condition);
}
}


OUTPUT

C# True
asp.net True
vb.net True
j# False
f# False
Cobol.net True
LINQ False
WCF False
WPF False
Silverlight True

LINQ Samples (Projection Operators) : Select Anonymous

Following example is used with Anonymous type for a select operation.It provides an transformed output as a Anonymous type


private void SelectAnonymousTypeOne()
{
string[] strTech = new string[] { "C#", "asp.net", "vb.net", "j#", "f#", "Cobol.net", "LINQ", "WCF", "WPF", "Silverlight" };

var str = strTech.Select(a =>
new { str = a.ToString(),
length = a.Length ,
specialChar = a.EndsWith("#")
});

foreach(var v in str)
{
Console.WriteLine("{0}\n Length:{1} Ends With #{2}",v.str,v.length , v.specialChar );

}
}

OUTPUT

C#
Length:2 Ends with char '#': True
asp.net
Length:7 Ends with char '#': False
vb.net
Length:6 Ends with char '#': False
j#
Length:2 Ends with char '#': True
f#
Length:2 Ends with char '#': True
Cobol.net
Length:9 Ends with char '#': False
LINQ
Length:4 Ends with char '#': False
WCF
Length:3 Ends with char '#': False
WPF
Length:3 Ends with char '#': False
Silverlight
Length:11 Ends with char '#': False

LINQ Samples (Projection Operators) : Select Transformation

Following example will obtain the odd string from there index position based on the value from a different array. If this is to be acheived by for loop; two for loops are required.


private void SimpleSelectTransformation()
{
int[] num = { 1, 3, 5, 7, 9};

string[] strTechn = new string[] { "C#", "asp.net", "vb.net", "j#", "f#", "Cobol.net", "LINQ", "WCF", "WPF", "Silverlight" };
var trans1 = num.Select(a => strTechn[a]);

foreach (string s in trans1)
{
Console.WriteLine(s);
}
}


OUTPUT:

asp.net
j#
Cobol.net
WCF
Silverlight

LINQ Samples (Projection Operators) : Example 2

Following example is used to obtain all product name from a list of 'Product'.

In the statement var v = prod.Select(a => a.productName); a is nothing but a placeholder or act as a parameter that represent object 'prod'.


private void SimpleSelectTwo()
{

List prod = new List
{
new Product() { productID=1, productName="Cooking Oil", productPrice=21.5, stockQuantity=10 },
new Product() { productID=2, productName="Washing Powder", productPrice=38, stockQuantity=10 },
new Product() { productID=3, productName="Tea bags", productPrice=110, stockQuantity=100 },
new Product() { productID=4, productName="Garbase bags", productPrice=21.5, stockQuantity=12 },
new Product() { productID=5, productName="Health drink", productPrice=85, stockQuantity=18 },
new Product() { productID=6, productName="Cool drinks", productPrice=45, stockQuantity=15 }
};
Console.WriteLine("\nBefore LINQ :");
foreach (Product pr in prod)
{
Console.WriteLine("ProdId: {0}, ProdName: {1}, ProdPrice: {2}, Stock: {3} ", pr.productID, pr.productName,pr.productPrice, pr.stockQuantity );
}

var v = prod.Select(a => a.productName);

Console.WriteLine("\nAfter LINQ : only Product is selected");
foreach(string pr in v )
{
Console.WriteLine("Product Name : {0}", pr);
}

}

OUTPUT

Before LINQ :
ProdId: 1, ProdName: Cooking Oil, ProdPrice: 21.5, Stock: 10
ProdId: 2, ProdName: Washing Powder, ProdPrice: 38, Stock: 10
ProdId: 3, ProdName: Tea bags, ProdPrice: 110, Stock: 100
ProdId: 4, ProdName: Garbase bags, ProdPrice: 21.5, Stock: 12
ProdId: 5, ProdName: Health drink, ProdPrice: 85, Stock: 18
ProdId: 6, ProdName: Cool drinks, ProdPrice: 45, Stock: 15

After LINQ : only Product name is selected
Product Name : Cooking Oil
Product Name : Washing Powder
Product Name : Tea bags
Product Name : Garbase bags
Product Name : Health drink
Product Name : Cool drinks

Sunday, April 25, 2010

LINQ Sample using lambda Expressions

The LINQ Samples are modification for the existing examples at 'http://msdn.microsoft.com/en-us/vcsharp/aa336758.aspx'.

The example here serve two purpose one is all are using lambda expression instead of the query used such that whoever is from a lambda expression background can understand those examples and secondly its more details for beginners.

Restriction Operators
Simple where 1
Simple where 2
Simple where 3
Simple where drill down
Simple where using indexed

Projection Operators
Simple select example 1
Simple select example 2
Select transformation
Select anonymous type example1
Select indexed
Select filtered
Select Compound 1 (combine two unrelated list)
Select Compound 2 (combine two unrelated list)

Partitioning Operators
Take simple and nested
Skip simple and nested
TakeWhile simple
SkipWhile simple
Take and Skip combined

LINQ Samples (Projection Operators) : Example 1

Example 1 : Provide a simple select which will allow to do a select on the list of string with a simple modification

private void SimpleSelectOne()
{
Console.WriteLine("Simple Select operations \n");
string[] strTechnology = new string[] { "C#","asp.net","vb.net","j#","f#","Cobol.net","LINQ","WCF","WPF","Silverlight" };

Console.WriteLine("\nBefore LINQ :");
foreach (string s in strTechnology)
{
Console.WriteLine("{0}", s);
}

Console.WriteLine("\nAfter LINQ transform : ");
var strList = strTechnology.Select( a => "MS " + a );
foreach (string s in strList)
{
Console.WriteLine("{0}",s);
}
}


OUTPUT :

BEFORE LINQ :
C#
asp.net
vb.net
j#
f#
Cobol.net
LINQ
WCF
WPF
Silverlight

AFTER LINQ transform :
MS C#
MS asp.net
MS vb.net
MS j#
MS f#
MS Cobol.net
MS LINQ
MS WCF
MS WPF
MS Silverlight


You can see that the statement var strList = strTechnology.Select( a => "MS " + a ); has added a string "MS" to existing string. Let it be reminded that the original string remains the same.

Thursday, April 22, 2010

Logging the progress of SSIS dataflow task

Progress of SSIS cannot be done using 'RowCount' Variable as value of a rowcount variable is set only after the execution of 'Data flow task'

We can acheive this however by using a script Component.

Read through the steps here ..