Friday 25 January 2013

LINQ to SQL – Select DISTINCT Values



·       First we have to create Data Context Class. Once Data Context is created we can query object model using    LINQ query.
·     
      First we have created Employee Table which has following Structure and Data.



In practical situations we will have to remove duplicate rows and get only unique rows for reporting. Assume we have the following data in the Employee Table.
In SQL Query to can use the DISTINCT clause to filter out duplicate rows as follows

SELECT DISTINCT First_Name, Last_Name, City
FROM  Employee

Now let us see how the same can be achieved using a LINQ query and bind data source in grid view control as following way.

HTML code in UI level



Output

Recover your Store Procedure from Cache


If the stored procedure has been executed, you can try recoving if from the cache with help of following query.

SELECT [cp].[refcounts],
               [cp].[usecounts],[cp].[objtype],
               [st].[dbid], [st].[objectid], [st].[text], [qp].[query_plan]
FROM sys.dm_exec_cached_plans cp 
CROSS APPLY sys.dm_exec_sql_text ( cp.plan_handle ) st
CROSS APPLY sys.dm_exec_query_plan ( cp.plan_handle ) qp

Simple Example of Delegate and When to Use


 Introduction:

A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature. A delegate is thus equivalent to a type-safe function pointer or a callback
The signature of a single cast delegate is shown below:
delegate result-type identifier ([parameters]);
Where:
  • result-type: The result type, which matches the return type of the function.
  • identifier: The delegate name.
  • Parameters: The Parameters, which the function takes.
Examples:
Following declaration define delegate name Add Pointer which will encapsulate any method that takes two objects as parameter and return an int.


When to Use:

·     I explained with following simple scenario I have class Called ClsMaths. it basically contain four method Add, Substract, Division and Multiplication. If suppose I have to call four method in Cls Maths class.  with following example we can get it.


·      Instead creating object every time new method will be added we can create pointer and pointer with matching argument point to ClsMaths method. So we can avoid heavy coupling between ClsMaths and UI code.

·       For your understanding I have created simple application. With help of operation we can get result either it would be addition, subtract, Multiplication or Division.
·        I have Create ClsMaths Class
·       For Presentation Layer code look like this.
·    If Suppose I pass one value in Operation Textbox it will call method Add, two for Subtraction like that.
Out Put:


 More about Delegates