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:
Friday, May 21, 2010

Two new profile in new visual studio 2010.

Visual studio 2010 is a great tool and i have become fan of visual studio 2010. I have found two new code profile in visual studio 2010.

  1. Web Development Profile
  2. Web Development Code Optimized Profile.

Profile

Web Development profile will hide the top bar which contains the client object and and event dropdowns. So it will have more spaces.

WebDevelopment

Another one web development code optimized which will hide all the things except main windows. It will hide Toolbox,CSS properties and all other things so you will have more spaces to play with your html.

Windows Code Optimized

So as a web developer you can use this two great new profile as per your convenience when you only want to play with your html then use webdevelopement code optimized profile and another interesting thing is that you don’t have to reset your settings you can also just do with Tools->Settings menu like below. This will swap different profile like below.

Settigns

Hope this will help you..

Shout it
kick it on DotNetKicks.com
Share:
Thursday, May 20, 2010

Using transactions with LINQ-to-SQL

Today one of my colleague asked that how we can use transactions with the LINQ-to-SQL Classes when we use more then one entities updated at same time. It was a good question. Here is my answer for that.For ASP.NET 2.0 or higher version have a new class called TransactionScope which can be used to manage transaction with the LINQ.

Let’s take a simple scenario we are having a shopping cart application in which we are storing details or particular order placed into the database using LINQ-to-SQL. There are two tables Order and OrderDetails which will have all the information related to order. Order will store particular information about orders while OrderDetails table will have product and quantity of product for particular order.We need to insert data in both tables as same time and if any errors comes then it should rollback the transaction.

To use TransactionScope in above scenario first we have add a reference to System.Transactions like below.

TransactionScope,System.Transactions

After adding the transaction we need to drag and drop the Order and Order Details tables into Linq-To-SQL Classes it will create entities for that. Below is the code for transaction scope to use mange transaction with Linq Context.

 MyContextDataContext objContext = new MyContextDataContext();
using (System.Transactions.TransactionScope tScope
= new System.Transactions.TransactionScope(TransactionScopeOption.Required))
{
objContext.Order.InsertOnSubmit(Order);
objContext.OrderDetails.InsertOnSumbit(OrderDetails);
objContext.SubmitChanges();
tScope.Complete();
}
Here it will commit transaction only if using blocks will run successfully. Hope this will help you.

Shout it
kick it on DotNetKicks.com
Share:
Tuesday, May 18, 2010

Google Translation API Integration in .NET

Language localization is one of important thing of site of application nowadays. If you want your site or application more popular then other then it should support more then language. Some time it becomes difficult to translate all the sites into other languages so for i have found a great solution. Now you can use Google Translation API to translate your site or application dynamically. Here are steps you required to follow to integrate Google Translation API into Microsoft.NET Applications.

First you need download class library dlls from the following site.

http://code.google.com/p/google-language-api-for-dotnet/

Go this site and download GoogleTranslateAPI_0.1.zip.

Then once you have done that you need to add reference GoogleTranslateAPI.dll like following.

AddReference,Google,

Now you are ready to use the translation API from Google. Here is the code for that.

string Text = "This is a string to translate";
Console.WriteLine("Before Translation:{0}", Text);
Text=Google.API.Translate.Translator.Translate(Text,Google.API.Translate.Language.English,Google.API.Translate.Language.French);        
Console.WriteLine("Before Translation:{0}", Text);
That’s it it will return the string translated from English to French. But make you are connected to internet :)… Happy Programming

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