May 27, 2009

Using let in LINQ to Objects – Performance killer if used wrong way

C# 3.0 LINQ has one more hidden and powerful feature; which provides you to store result of sub-expression in order to use subsequence clause. You can achieve this with the help of let keyword. The variable created using let is readonly; once initialized it can’t use to store another value only good this is it can be queried.

Let’s see this with example. We have employees details in text file delimited my ‘:’, also each employee have it’s own details separated by ‘,’.

string strEmployees = "1, John, Methew:2, Nick, Althoff:3, David, Oliver:4, Sam, Peterson";

Lets write query to grab all the employee details.

string strEmployees = "1, John, Methew:2, Nick, Althoff:3, David, Oliver:4, Sam, Peterson";

//Split with :
var query = from empData in strEmployees.Split(':')
select empData;

//Split with ,
foreach (var q in query)
var e = q.Split(',');
Console.WriteLine("Id - {0}, First Name - {1}, Last Name - {2}",
e[0], e[1], e[2]);

As you can see we have to write two different logic to split one more time employee details, now let’s use let keyword and make coding easy.

string strEmployees = "1, John, Methew:2, Nick, Althoff:3, David, Oliver:4, Sam, Peterson";

var query = from empData in strEmployees.Split(':')
let emp = empData.Split(',')
select new { Id = emp[0], FName = emp[1], LName = emp[2] };

foreach (var q in query)
Console.WriteLine("Id - {0}, First Name - {1}, Last Name - {2}",
q.Id, q.FName, q.LName);

In both query output will be same.


emp is intermediate enumerable type which we are using in next line! This is very simple example, now what compiler treats the code above? Compiler will create one sub-query that returns the anonymous type composed of the original value along with new value specified by the let.

As its creating sub-query if you write bunch of let statements, it will kill your performance. If its implemented in proper way let is very good option to go with, the scenario where you need some function which operates on your select clause more then 2-3 times, you can create let variable and use them into your select which makes your faster.

static void SummOfferNoLet()
var q = from c in Products
where SumOffers(c) < 10000 && SumOffers(c) > 1000
select c;
int count = q.Count();
static void SummOfferWithLet()
var q = from c in Products
let offerValue = SumOffers(c)
where offerValue < 10000 && offerValue > 100
select c;
int count = q.Count();

In this case SummOfferWithLet will faster as you can see SummOfferNoLet we need to call SumOffers twice.

Conclusion: Using let is powerful but if you used wrong way then it will kill the performance.

May 25, 2009

Issues with Web.config in IIS 7 and Modules (in Vista)

A simple web application runs fine on internal web server, but the time we put it on IIS7 specially in Windows Vista, its start giving configuration error, first and foremost is issue with Modules.

This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false".

And its points to <handlers> section under <system.webServer> section. The issue related to IIS don’t have ASP.NET installed. You can check your Windows feature, although you have installed IIS7 and compatibility for IIS6, ASP.NET is not getting installed automatically. Here is the Windows feature should look like as in following image.


You can find this window from Control Panel –> Programs and Features –> Turn Windows features on or off [Left panel]


If you have ASP.NET installed on your IIS 7.0 then you have to change the configuration from applicationHost.config file, which resides in %windir%\system32\inetsrv\config\applicationHost.config. You can find the entry for handlers and it has value Deny for property overrideModeDefault, change it to Allow.

<section name="handlers" overrideModeDefault="Deny" /> to <section name="handlers" overrideModeDefault="Allow" />

While saving file I am pretty much sure that it asks for Administrator account although your role is Administrators as you are not owner of that file you can’t make change to file, so solution for this is login as Administrator and do the changes. For vista Administrator is not active for login so find my post which help you to login as Administrator.

May 23, 2009

How to login as Administrator in Windows Vista

Hi All,

In working with Visa, you always get Alert saying “You don’t have permission to access this folder, click continue to get access”, or “Windows needs your permission to continue” or “Destination Folder Access Denied” or due to security you are not able to save file or change who owned by System…. Lots more. Although you have Administrative privileges still its says sometime “You should have administrative permission”. Or if a program needs Admin permission then you can run that application using “Run as Administrator”!!!!

So we need to do login using Administrator account, but the question is from where?

In Vista, the Administrator (or an administrator) is no longer the most trusted object in the operating system. Yes this is to "ostensibly" protect the system, and is part of a general concept of protecting the integrity of the system. The Administration is not activated and you don’t find any User Interface [Up to now, I didn’t fine] which make it active!!! Does it means you can’t make it Active? NO.

To make it active, you have to run command prompt with Administrator permission. Go to Start –> All Programs –> Accessories –> Command Prompt [right click and RUN AS ADMINISTRATOR]


Write following commands to make Administrator active and set the password.


First command will make Administrator account active and another will set yourpassword as Administration account password. Last command Exit.

Now, reboot or switch user or logoff from current user; you can see it will ask for Administrator account password.


This application has failed to start because the application configuration is incorrect – Solution

I have migrated Vista Home to Vista Premium and my SQL Server and Visual Studio 2008 was not able to start and giving me error ‘This application has failed to start because the application configuration is incorrect, the application has failed to start because its side-by-side configuration is incorrect’. I then check the Event Viewer and find some Dependent Assembly were missing.

Activation context generation failed for "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\LanguagePackage.dll". Dependent Assembly Microsoft.VC80.CRT,processorArchitecture="x86",publicKeyToken="1fc8b3b9a1e18e3b",type="win32",version="8.0.50727.1833" could not be found. Please use sxstrace.exe for detailed diagnosis.

Solution: If you are using VS2005 then download vcredist_x86.exe, and for 2008 download vcredist_x86.exe. No need to do any registry entry or any manifest file.