Sunday, 6 October 2013

Collections in C#

Collections are group of records which can be treated as single logical unit. Collection classes are specialized classes for data storage and retrieval.Collection classes are derived from System.Collection namespace
Dot net  collections are divided into four categories:-
1. Indexed based
Indexed based collections helps to access the value of the row using internally generated index by the collections. They are two types:-
Array
List
2. Key value pair
Key value pair based collections helps to access the value of the row using user defined key. They are two types:-
Hashtable: - Uses a key to access elements in the collections
Sorted list: - Uses a key as well as an index to access elements in a list. It is a combination of an array and a hashtable.If we access items using an index, it is an arraylist and if we access the items using a key, it is a hashtable. The collection of items is always sorted by the key valu e.
3. Prioritized collections
Stacks
Queue
Queues and stacks helps to access data in a particular sequence.
Queues uses FIFO(First in First out) methodology.
Stacks uses LIFO(Last in First out) methodology.
4. Specialized collection
String collections

Hybrid dictionary

Thursday, 3 October 2013

Lamda Expression in C#

The concept of lamda expression was introduced in C# 3.0. It is just a new way to write anonymous methods. At compile time all the lamda expressions are converted into anonymous methods according to lamda expression conversion rules. The left side of the lamda operator "=>"(is called ‘goes to’ operator) represents the arguments to the method and the right side is the method body.

Lambda expression Syntax:
(parameters) => expression-or-statement-block

Types of Lamda expression

1. Statement Lamda
Statement lambda has a statement block on the right side of the lambda operator "=>".
Eg: a => {return a * a;};

       
2. Expression Lamda
Expression lambda has only an expression .Will not have a return statement or curly braces, on the right side of the lambda
operator "=>".
Eg: a => a * a; // here a*a is expression


Lets take an example :

Lets convert it to lambda expression

X=>x+2 is the lambda expression. The left side of the lambda operator specifies the input parameters and the right side holds the expression or statement block.

Hope you are enjoyed this.


Tuesday, 10 September 2013

Ranking Functions in SQL


Ranking functions are used  for statistical ordering. In this Article, you learn how to use the four ranking functions available in the SQL Server 2008.
The four ranking functions are:-

1.    ROW_NUMBER
2.    RANK
3.    DENSE_RANK
4.    NTILE


1.    ROW_NUMBER

ROW_NUMBER assigns a sequential number from 1 to n to the rows based on user specified sorting order.

Syntax:

ROW_NUMBER() OVER (ORDER BY <column Name>)

Example :
Consider a table tblStudent with columns ‘Name’ and ‘Marks’.



Using ROW_NUMBER() function

SELECT Name,
                Marks,
                ROW_NUMBER()OVER (ORDER BY Marks) AS RowNo
FROM tblStudent

RESULT:



2.    RANK
This function does much the same thing as ROW_NUMBER.But if there are same values in the record, it is said to be a tie and these records get the same rank.

Syntax:

RANK() OVER (ORDER BY <columnName>)


Example :
Consider the same table tblStudent.

Using Rank function
SELECT Name,
                Marks,    
                RANK() OVER (ORDER BY Marks) AS RankNo
FROM tblStudent
RESULT :


3.    DENSE_RANK

DENSE_RANK assigns same value to the each duplicate records, but does not produce gaps in the sequence.

Syntax:
         DENSE_RANK() OVER (ORDER BY <columnName>)

Example :
Consider the same table tblStudent.
Using Dense_Rank function
   SELECT Name,
                Marks,    
                DENSE_RANK() OVER (ORDER BY Marks) AS DenseRankNo
FROM tblStudent

RESULT:


4.    NTILE

NTILE is used to split the result into approximately equal groups.If you want a result to split into 4 equal groups,you can use NTILE(4).

Syntax :

NTILE(integerExpression) OVER (ORDER BY <columnName>)

Example :
Consider the same table tblStudent.
Using NTILE Function