Wednesday, June 23, 2010

Range Operator in Linq.

Linq is almost providing all the functionalities and i have found one another great operator called range operator which will return a sequence of integer number from start point to number of count. Here is the signature of range operator in Linq.
public static IEnumerable<int> Range(int start, int count)
Here the Start means the starting integer of the sequence and count means the number of sequence you want from starting integer. Let’s take a simple example for that where will print 5 to 9 sequence with the help of range operator. Here is the code for that.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        var RangeResult = Enumerable.Range(5, 5);
        Console.WriteLine("Range Result");
        foreach (int num in RangeResult)
        {
            Console.WriteLine(num);
        }
    }

}
}
And here is the output for that as expected.

LinqOperators
Hope this will help you..

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:
Tuesday, June 22, 2010

Persisting row selection in data bound control like GridView,ListView

Some times we need show large amount of data like hundred of rows but its very difficult to show that in a single web page so we are using the paging mechanism of inbuilt data bound control of .NET Framework like GridView and ListView. But in paging of data bound control its very hard to remember the row selection like suppose you have selected a row in page 1 and then you goto the page2 and return back to page1 the selected row will not be there. So to do that thing you have right custom coding for that. In Microsoft .NET Framework 4.0 there is one property called EnablePersistedSelection which will automatically do that task. It will first introduced on dynamic data support in .NET Framework 3.5 sp1 but its bye default supported by default in .NET Framework 4.0 it self. In earlier version of .net framework the selection was based on row index now its will be based data key for data bound controls. Here is the code for that.

<asp:GridView id="PersitantGridView" runat="server" EnablePersistedSelection="true">
</asp:GridView>
Its based on datakey so now you go to page1 and selected third row and then go to the page 2 and selected second row and now you again go to first page and you will have third row selected. That’s it.. Hope this will help you..

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:
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:

Union,Except and Intersect operator in Linq

While developing a windows service using Linq-To-SQL i was in need of something that will intersect the two list and return a list with the result. After searching on net i have found three great use full operators in Linq Union,Except and Intersect. Here are explanation of each operator.

Union Operator: Union operator will combine elements of both entity and return result as third new entities.

Except Operator: Except operator will remove elements of first entities which elements are there in second entities and will return as third new entities.

Intersect Operator: As name suggest it will return common elements of both entities and return result as new entities.

Let’s take a simple console application as a example where i have used two string array and applied the three operator one by one and print the result using Console.Writeline. Here is the 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" };
         string[] b = { "d","e","f","g"};

         var UnResult = a.Union(b);
         Console.WriteLine("Union Result");
         foreach (string s in UnResult)
         {
             Console.WriteLine(s);           
         }

         var ExResult = a.Except(b);
         Console.WriteLine("Except Result");
         foreach (string s in ExResult)
         {
             Console.WriteLine(s);
         }

         var InResult = a.Intersect(b);
         Console.WriteLine("Intersect Result");
         foreach (string s in InResult)
         {
             Console.WriteLine(s);
         }
         Console.ReadLine();
        
     }

 }
}

Here is the output of console application as Expected.

LinqOperators

Hope this will help you.. Stay tuned for more!!


Share:
Saturday, June 12, 2010

ASP.NET4.0-Compatibility Settings for rendering controls

With asp.net 4.0 Microsoft has taken a great step for rendering controls. Now it will have more cleaner html there are lots of enhancement for rendering html controls in asp.net 4.0 now all controls like Menu, List View and other controls renders more cleaner html. But recently i have faced strange problem in rendering controls I have my site in asp.net 3.5 and i want to convert it in asp.net 4.0. I have applied my style as per 3.5 rendering and some of items are obsolete in asp.net 4.0. Modifying style sheet was a tedious job here asp.net 4.0 compatibility setting comes into help.

Asp.net 4.0 compatibility settings provides full backward compatibility in terms of the rendering controls. You can assign this in your web.config section like following.

<system.web>
<pages controlRenderingCompatibilityVersion="3.5|4.0"/>
</system.web>
Here the values of controlRenderingCompatibility is a string which will indicate on which way control should render in browser if you provide 4.0 then it will controls with more cleaner html and while if you want to go with old legacy rendering like 3.5 then you can put 3.5 and it will render same way as you are doing in asp.net 3.5.

Hope this help you!!!

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

ASP.NET 4.0- CompressionEnabled Property in session state 4.0

Hello Guys,

This blog has been quite for few days. Because i was busy with some personal and professional work both and that’s why i am not able to work on writing blog posts which i have discovered in last few days. Here is one features of asp.net 4.0 that I am going to explain.

As a web developer we all know about session. Without the use of session any database driven web application is incomplete. As we all know unlike windows form web forms are state less so when user interacts with web application we need to maintain state amongst web pages and we are using session for maintaining state between web pages for each users. ASP.NET is also provide same kind of session state functionalities. ASP.Net Session state identify request coming for same user and same browser for specific session time out interval and its preserves values in session for that specific time intervals and that’s help us in maintaining state amongst web pages for a specific user.

ASP.NET Session state allows us to store session in three way 1. IncProc 2. Session State Service 3. SQL Server. In SQL Server mode it will store session in SQL Server tables instead of storing it in Server Memory. ASP.NET 4.0 provides a new property called Compression Enabled that means when we store values in serialized form in SQL Server with GZip Compression and that results in better performance.

For that you need to store property in web.config like following.

<sessionState allowCustomSqlDatabase="true" sqlConnectionString="data source=Server;Initial Catalog=aspnetsessionstatedb" compressionEnabled="true" />
That’s it now with the use of this property you can have better performance when you are storing large amount of data in session.But still you need to decide that why you want to stored large amount of data in session because its against best practices.

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