Lambda Expression Usages - some basic examples

Hi,

Continuing from my previous post in this post I will put up some examples of the places and the way we can use Lambda Expression. I will post all the examples based on the where clause of the LINQ query on arbitrary collection of integer, string.

static int[] numbers = new [] { 0,1,2,3,4,5,6,7,8,9 };

static string[] strings = new [] { "a","b","c","d","e","f","g" };

class Employee
{

      public string Name {get; set;}

      public int Salary {get; set;}

}


To filter out only those values which matches a certain condition


var Vnums = numbers.Where(int n => n > 4);

To filter out only those values from the collection of string which matches a certain condition

var v = strings.

Continued...

Lambda expression – How to start with the basic of Lambda expression

Hi,

One of the new features of C Sharp 3.0 is Lambda expression. Lambdas are great new thing from language prospective. But for a lower or middle level developer they are not that simple. Actually a common Dot net, C Sharp, developer is not used to the kind of the expression. 

For a normal developer Lambda expressions will be a bit difficult in the beginning. But believe me when you have had a little bit of understanding on how to start with Lambda then You will understand the power of it,

In this post I will show on writing some of the Lambda expression. I hope these examples helps on understanding the Lambda Expression. Let me start with how a Lambda expression is made.,

A Lambda is expression is created by providing the parameter and then the => keyword and then the expression. ,

Lets say we want a function were we will pass the integer and

Continued...

CompilationMode enumeration for the page tag in asp.net 2.0
Hi,

One of the new attribute of the page tag in Asp.Net 2.0 is the CompilationMode attribute. The attribute is used to specify whether the Asp.Net compiler should compile the page or not at the runtime. The attribute has been added new in the asp.net 2.0.

The attribute can have 3 possible values. Always, Auto and Never. The default value is Always. As the name suggest the always and never value are used to tell the compiler to compile the page always or never respectively. Auto value means that Asp.Net will only compile the page if required.

Setting a page to never compile using the Never attribute will increase performance by disqualifying the specified page in the compilation process. Here is how we set the attribute for a particular page.
<%@ page compilationMode=”never”
Continued...

Writing Inner join in LINQ queries

Hi,

I have been playing with LINQ from quite a few days I should say I am impressed with it. It does nearly everything out of box. Yes nearly every thing out of box. And the ORM is also very very powerful. In This post I will illustrate how to use Joins in LINQ queries.

Many a times we want a query where by we retrieve the data from one table and some the related data from the other table. Let says we have a category table and a posts table. Now when I retrieve all the records of the posts I also want to have the related category name (which is there in the category table). So I need to make a join between three tables to get the records. Here is the LINQ Query to do the job.

var t = from p in BlogBLL.Posts

            join cp in BlogBLL.CategoryPosts on p.PostId equals cp.PostId

Continued...


Option group control now with indentation for group member

Hi,

Here is an update to the option control that I posted a few days ago. The control allows you to show group in dropdown list and list box control, where by the user can only select the group member and not the group header. Read this post for more detail on the usage of the control.

The control had a bug where by the options would not indent properly. The problem was in the rendering of OptGroup tag. The tags needed to end only after all the option of group have been rendered. Special thanks to Tim who pointed and also provided the code for the fix of the issue.

This time around I have also given the full source code as download. Many people asked me to give the source code, as it is difficult to use such controls in live without having the source code.

Here is the link

Continued...

SQL Server 2008 Katmai – List of new features

Hi,

It’s been quite a few days that SQL server 2008 (Katmai) CTP has came out. SQL server 2008 is being released with Visual studio 2008 and Longhorn server. There are many new features in the new version of SQL Server.

Here are some of the new features (Categorised) of the SQL server 2008.

Security and Data Auditing

Transparent Data Encryption

Data Auditing

Availability and Reliability

Continued...


A blue professional theme for the site

Hi

After a very long time I am adding a new theme to the site. It’s been long and speaking truly I did stopped to think about adding anymore theme. But then I saw the designer in our group make some template in her free time. I started enjoying one of them. And that was the beginning of the thought for the new theme.

The look was very raw then. But I liked what I was seeing. I then asked her to give me the complete template so that I can use it in my site. She is pretty sweet to be so gentle and kind to do it in her free time. I have to say Thanks You Rimjhim.

But the big work of doing the HTML of the Site was still left. I went back to my old (Does not mean she is

Continued...

New Lambda Expressions in C Sharp 3.0

Hi,

One of the new features of C Sharp 3.0 is the Lambda expression. As we all know C Sharp 2.0 introduced the concept of anonymous method. An anonymous method allows code methods to be written inline instead of delegate value. A lambda expression provides a more concise way for writing anonymous methods for functional syntax.

Lambda expressions are very useful when used with (in) LINQ queries. A lambda expression provides a type safe and compact way to write which can be passed as argument for subsequent evaluation.

The syntax to write a lambda expression is very simple. The syntax of a lambda expression would be parameter list followed by => token and then the expression block that needs to be executed when the expression is invoked.

Parameter => expression


Lambda expression also allows parameter to be omitted from

Continued...

Getting all the methods in a type using LINQ

Hi

One of the most beautiful thing about the new LINQ queries in the Dot net 3.5 is the simplicity with which it lets you query any collection. Yesterday I was playing with the LINQ queries and suddenly a requirement came to me where by I wanted to display all the methods that string (Or any other class you want to choose) class has.

I know this can be done through reflection very easily. But the good thing about LINQ is the simplicity and small amount of code with which I could get the Job done.

All I had to do was declare an object of type “Type”. Assign the object the class I want to use. And use a very simple LINQ query to get all the methods in a collection. Here is the code.


Type tStr = typeof(string);
            var ms = from method in tStr.GetMethods()

Continued...


Completing one year of blogging with 200 blogs

Hi,

Guess what, I have Completed a year in blogging....and what better reminder than my Service provider asking me to renew my domain registration which actually means to pay my bills!! So it's a year in the blogging world. When I look back at the last year I see myself starting the blog with a very simple theme, adding 4 more theme to the site. Starting with a plain Site and adding lots of Ajax functionality to the site. I had some non technical blogs also but overall the blogs were directly or indirectly related to Dot Net, Asp.Net and SQL Server.

There are a total of 200 post in the last year(I targeted the same for a year time frame). This is my 201th post. In the Mean time I have also started a small fun site were I blog about the fun stuff (Jokes, Information etc...).

Continued...


LINQ the new way to query How to make basic queries

Hi,

One of the best new things in the new Dot net 3.5 is the LINQ. LNQ stands for Language Integrated Query. With the help of LINQ now we can query nearly anything in a more than efficient manner. It also gives us compile time check to the inline query’s, which reduces the chances of runtime error.

A simple query of the LINQ start with the “from” clause and ends with the select clause. This is quite opposite of what we are used to in the SQL (where we have the select clause first and from clause later). Below is the simple example of a Linq Query.

var t = from c in BlogBLL.Categories

               select c;

Here I am selecting all the categories from the data context BlogBLL. The BlogBLL is a data context that contains the mapping of all the tables. If we want to

Continued...

C Sharp 3.0 and Anonymous Types

Hi,

I have been using the new Orcas Beta 2 and dot net frame work for some time now and have been really enjoying the features of the C Sharp. One of the new features also includes Anonymous types. It first thought there might not be much use of this feature and will only be a syntactic sugar for us. But after playing with Dot net framework 3.5 for a while I understand the importance of this new feature. Let me also tell that this feature will be most useful in case you are querying through the new LINQ (Language Integrated Query).

With the help of the anonymous types we can define a type without declaring it from before hand. The main difference between the defined types and anonymous types is that we leave the type name in the anonymous types. The compiler will parse the syntax and create a standard CLR type itself for the definition of the type. Here is an example of an anonymous type.

var blog =  new

Continued...

Dot net 3.5 Extension method and its usability

Hi,

Now when I have started working on dot net 3.5 the feature I love most is the extension method. I know many of you will be surprised that it’s not LINQ or the other new feature that are there in visual studio but this one. Well let me tell you how it let me code the way I want to (without leaving me to the limitation of the C sharp).

The kind of coder I am I use the Response.write() a lot. Whenever debugging some problem, for each response I have to add this line. But with extension methods I can add a new extension method to the object class to get this done pretty easily (and make my code look better).

Here is a simple extension method I created to help me out.

public static void Response(this object obj)

{
       Response.write (obj);
}

Now I do not have to write Response.Write.

Continued...

Use HttpWebRequest to get the status on weather a site is responding

Hi,

Yesterday I was working and found that one of my projects was not responding from time to time for some unknown error. The problem was intranet (some network problem with database server). We were loosing connection from time to time. But I wanted to keep a check if the site is running or not. So what I basically wanted was a check if the site was working fine by some other entity, which would tell me when there is problem with the web site.  

Hence I decided to make a small windows application that would check for a given URL at the given difference of time and would report back if the site is responding. I found it very useful so thought of sharing with all.

The main work that had to be done was to make a request to the site and check if the response status code is 202 (That is the status code when the site is running fine). Here is the code on how this was done.

Continued...


Order of Compilation for the asp.net 2.0 application

Hi

We all know the term compilation in Asp.Net. But do you know what is the order of compilation in an asp.net project. Do you know which folder in the application structure gets compiled first and which when get compiled last?

Here is the compilation life cycle of an Asp.Net project in the order they get compiled.

App_GlobalResources – First of all the global resources are compiled and the resource assembly is built. All the assembly’s in the bin folder are linked to this assembly.

App_WebResources – next the web resource folder will be compiled. This will create all the proxy types for the web services. The web reference created is then linked to the resource assembly (if it exists.).

Profile properties (defined in the Web.config file) – If we have defined the profile properties in the web.config file than an assembly will be generated for the profile

Continued...

Microsoft provides first reliability and performance update for vista

Hi,

Microsoft released the first performance and reliability update for vista. Exactly what has been updated is not clear. It’s supposed to increase the performance and the reliability of the system. I would suggest everyone using Vista to download and install the patch.

Here is the link to the download.

Windows Vista Performance Update:


Finding Stored procedure containing the given text.
Hi,
Many a times we change some filed name or a table name in the database. This causes to change all the stored procedure that uses the field. Its can be very easily done if we can get the name of all the stored procedure contains the given text. (It can be bit difficult in case of column name as 2 table can have the same column name. So we need to track it with the help of the table name, unless we are sure that the column name is unique in the database.)
Here is a simple query that gives all the procedure names that contains the given text.
SELECT ROUTINE_NAME, ROUTINE_DEFINITION
                
Continued...

Application restarts 7 reasons that should be avoided

Hi

There are many reasons why the Web Application restarts in Asp.net 2.0. If you are using IN Proc (which is the fastest and most used) then all the users will loose the session information. So it’s very important to know what can cause an application restart in the web application. If we do not stop this application restart process it can cause great problem, as all the user will start loosing their session in the middle of nothing.

Here is a list of events when the Asp.net application restarts in Asp.Net 2.0

  • Adding, modifying, or deleting assemblies from the application's Bin folder.
  • Adding, modifying, or deleting localization resources from the App_GlobalResources or App_LocalResources folders.
    • Adding, modifying, or deleting the application's Global.asax
Continued...

Visual studio 2008 C Sharp Organize using statement

Hi,

I have just started looking in the Visual studio 2008. One of the things that I have already started to like very much is the using statement organization. Yes Now we can select all the using statements and ask the studio to organize it. The studio will sort the statement in the order of their name.

The better thing was the ability to remove the unused using statement. Yes you read it right. Select the using statements rightclick->OrganizeUsing->Remove Unused Usings. This will remove all those using statements that are not required in the page.

What a beautiful utility. Something that will improve the overall code of a normal developer.

VikramP.S. Stay tuned to this location for more news on the visual studio 2008


How to deactivate the Windows Vista Aero color scheme

Hi,

Form last few weeks I have been playing around with Vista. I love the Areo glass theme, but sorry to say it takes way too much of memory to continue for me. Now with Visual studio 2008 Beta also installed, it’s not possible to run Aero on my system.

So I decided to turn of the Areo system. But I found it quite difficult to find information on deactivating the Areo Glass system. So I decided to put the info myself.

  • Open the personalization section of the computer. You can do this by right clicking on any blank spot on desktop and choosing Personalisation.
  • The first link will be, Window Color and Appearance. If you are using Vista Aero then click on the last link (Open classic appearance properties…). The Appearance Settings dialog box opens.
  • In the colo0r scheme list choose another scheme(other than
Continued...
 
Copyright © 2006 - 2012 Vikram Lakhotia