Saturday, June 19, 2010

Zip operator in Linq with .NET 4.0

Microsoft .NET framework 4.0 is having many features that make developers life very easy. Its also provides some enhancement to Linq also. I just found a great operator called Zip which merge the sequence of two entities.

Here is the explanation of Zip Operator from MSDN.

“The method merges each element of the first sequence with an element that has the same index in the second sequence. If the sequences do not have the same number of elements, the method merges sequences until it reaches the end of one of them”.

Here is the simple console application for the zip. I have taken three arrays for that one is string array, second one is integer array which is having same length as string array and third one is also integer array but having different length then string array. Here is the sample code for that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] a = { "a", "b", "c", "d" };
int[] num = { 1, 2, 3, 4 };
int[] numdiff = { 1, 2, 3, };


Console.WriteLine("Zip Exmpale with same length");
var ZipResult = a.Zip(num,(ae, ne)=>ae + ", " + ne.ToString());
foreach (string s in ZipResult)
{
Console.WriteLine(s);
}

Console.WriteLine(@"\n\n\nZip Exmpale with diffrent length");
ZipResult = a.Zip(numdiff,(ae, ne)=>ae + ", " + ne.ToString());
foreach (string s in ZipResult)
{
Console.WriteLine(s);
}

Console.ReadKey();
}

}
}
Here is the output of following code as expected.Linq Zip Operator in .NET 4.0

Hope this will help you.

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:

Sum and Concat Operator operators in Linq.

Linq contains lots useful operators and i have found more two operators that can be help full in our day to day programming life. Here are explanation.

Concat Operator: Concat operator concats two entities into single entities its very use full when we are doing some string operator where need to do string operations like concatenation. Here in example i have taken two arrays and concat into single entity.

Sum Operator: When we are developing application like shopping cart and other stuff this operator can be use full where we need to calculate the total or order price and other stuff you can apply this operator array, list and anything that implemented IEnumerable interface. Here i have taken a integer array for the example.

Here is the code for both operators.

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] a = { "a", "b", "c", "d" };
string[] b = { "d","e","f","g"};


Console.WriteLine("Concat Example");
var CtResult = a.Concat<string>(b);
foreach (string s in CtResult)
{
Console.WriteLine(s);
}

int[] num = { 1, 2, 3, 4, 5 };
Console.WriteLine("\n\n\nSum Example");
var SumResult = num.Sum();
Console.WriteLine(SumResult);
Console.ReadKey();
}

}
}
And here is the output of console application as expected.LinqOperators

Hope this will help you..

Technorati Tags: ,,,
Shout it
kick it on DotNetKicks.com
Share:
Friday, June 18, 2010

Take,Skip and Reverse Operator in Linq

I have found three more new operators in Linq which is use full in day to day programming stuff. Take,Skip and Reverse. Here are explanation of operators how it works.

Take Operator: Take operator will return first N number of element from entities.

Skip Operator: Skip operator will skip N number of element from entities and then return remaining elements as a result.

Reverse Operator: As name suggest it will reverse order of elements of entities.

Here is the examples of operators where i have taken simple string array to demonstrate that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] a = { "a", "b", "c", "d" };


Console.WriteLine("Take Example");
var TkResult = a.Take(2);
foreach (string s in TkResult)
{
Console.WriteLine(s);
}

Console.WriteLine("Skip Example");
var SkResult = a.Skip(2);
foreach (string s in SkResult)
{
Console.WriteLine(s);
}

Console.WriteLine("Reverse Example");
var RvResult = a.Reverse();
foreach (string s in RvResult)
{
Console.WriteLine(s);
}

}
}
}
Here is the output as expected.LinqOperators

hope this will help you..

Technorati Tags: ,,,
Shout it
kick it on DotNetKicks.com
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews