October, 2007

  • Wriju's BLOG

    LINQ to SQL : Paging Data

    • 11 Comments

    When we work with relational database and especially when with larger data we generally go for custom paging. This allows us to create superfast application.

     

    The simple logic for paged query is like,

     

    Let’s suppose you have 100 rows. Now you want to get data of page size 10 for page 2.

     

    So the starting record would be

     

    StartRecord = (PageNumber – 1) * PageSize

     

    For page 2 it would be

     

    StartRecord = (2 – 1) * 10 = 10 (may be you need to +1 to skip one record)

     

    Now when this comes to LINQ to SQL scenario remains the same where the implementation is little different.

     

    NorthWindDataContext db = new NorthWindDataContext();

     

    var query = from c in db.Customers

                select c.CompanyName;

     

    //Assuming Page Number = 2, Page Size = 10

    int iPageNum = 2;

    int iPageSize = 10;

     

    var PagedData = query.Skip((iPageNum - 1) * iPageSize).Take(iPageSize);

     

    ObjectDumper.Write(PagedData);

     

    Generated T-SQL,

    For SQL Server 2000 the provider does not support Skip().

     

    For SQL Server 2005

    ===================

    SELECT [t1].[CompanyName]
    FROM (
        SELECT ROW_NUMBER() OVER (ORDER BY [t0].[CompanyName]) AS [ROW_NUMBER], [t0]
    .[CompanyName]
        FROM [dbo].[Customers] AS [t0]
        ) AS [t1]
    WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p0 + @p1
    ORDER BY [t1].[ROW_NUMBER]
    -- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [10]
    -- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [10]
    -- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30428.1

     

     

    Namoskar!!!

  • Wriju's BLOG

    Visual Studio 2008 Automatic Property is the default snippet

    • 18 Comments

    Now if you type prop and hit "tab" twice, this will bring the Automatic property not the conventional property declaration.

    This is my new finding.

    Namoskar!!!

  • Wriju's BLOG

    C# 3.0 : Anonymous Type and .NET Reflection Hand-in-Hand

    • 2 Comments

    Anonymous Type is nothing but the type which gets defined by CLR (not you). So that type is as rich as any other normal type. Probably the conventional way to find the components of an Anonymous Type through reflection would be clearer to us,

     

    static void Main()

    {

        //Anonymous Type

        var anyType = new

        {

            IntID = 1,

            StringName = "Wriju"

        };

     

        Type t = anyType.GetType();

     

        PropertyInfo[] pi = t.GetProperties();

     

        foreach (PropertyInfo p in pi)

        {

            //Get the name of the prperty

            Console.WriteLine(p.Name);

        }

     

        //Using LINQ get all the details of Property

        var query = from p in t.GetProperties()

                    select p;

     

        ObjectDumper.Write(query);

    }

      

     

    Namoskar!!!

     

  • Wriju's BLOG

    C# 3.0 : Anonymous Types

    • 6 Comments

    C# 3.0’s Anonymous Type is a cool feature and it allows us to create object and collection of no type. Actually the type does not exist but on the fly you can create object out of that type. Let’s say you need to create some object which will have two properties Id of type int and Name of type string. In normal scenario, you have to write a class and then create the object of that class. But C# 3.0 allows us to declare an object of that type and assign value to it.

     

    Suppose you need this Class

     

    public class Cust

    {

        public int CustId { get; set; }

     

        private string _CustName;

        public string CustName

        {

            get { return _CustName; }

            set { _CustName = value; }

        }

    }

     

    But you are too lazy to create the class. This C# 3.0 will create this class for you. So if you write something like

     

    var obj = new { CustId = 1, CustName = "C#" };

     

    OneTypes

     

    Notice here after the keyword “new” I have no type name. This gives instruction to the compiler to create a dummy type for me. Now Visual Studio is so exciting that this does not require me to compile to be able to provide me the IntelliSense support. Without compiling I can get the IntelliSense and auto complete.

     

    Whenever I will use obj and after the “.” Visual Studio 2008 IntelliSense will show me the two properties,

     

    Img1

     

    One of the drawbacks is that you cannot reset the property value. So if you try to write, obj.CustId = 1; will through you an error.

     

    Property or indexer 'AnonymousType#1.CustId' cannot be assigned to

    -- it is read only  

     

    This is because it creates get-only property and can only be initialized through the Object Initializers feature of C# 3.0.

     

    Another observed behavior is that if you create object with the keyword “new”, like,

     

    var obj = new { CustId = 1, CustName = "C#" };

     

    var obj2 = new { Id = 1, Name = "C#" };

     

    TwoTypes

     

     

    Now the compiler will create two anonymous types as the property name differs.

     

    But….

     

    If you use objects like,

     

    var obj = new { CustId = 1, CustName = "C#" };

     

    var obj1 = new { CustId = 1, CustName = "C#" };

     

    Compiler will not create multiple types as the name is same.

     

    But!!!

     

    Interestingly if you have declarations like,

     

    var obj = new { CustId = 1, CustName = "C#" };

     

    var obj1 = new { CustId = "A", CustName = "C#" };

     

    var obj2 = new { CustId = 2.2, CustName = "C#" };

     

     

    The CLR will create only one Type, but the compiler will give you three different date type options for the three objects.

     

    Img2

    For obj the CustId will be inferred as type “int”.

     

    Img3

    For obj1 the CustId will be inferred as type “string”.

     

     

    Img4   

    For obj2 the CustId will be inferred as type “double”.

     

    This is because the generated IL creates the type as Generic type. With the

     

    .field private initonly !'<CustId>j__TPar' '<CustId>i__Field'

     

    "initonly" is readonly and "!" indicates that the type is generic.

     

    Namoskar!!!

  • Wriju's BLOG

    Visual Studio 2008 : How to add Visualizer for LINQ

    • 6 Comments

    There are two very important visualizers for LINQ which are not part of default installation.

     

    The visualizers are

    Ø       ExpressionTree Visualizer

    Ø       SqlServerQuery Visualizer  

     

    Both the visualizers are available from

     

    Visual Basic

    Samples for Visual Studio 2008 Beta 2

     

    Visual C#

    LINQ and language samples for Visual Studio 2008 Beta 2

     

    Once you install them you will find the ExpressionTree Visualizer and SqlServerQuery Visualizer under

     

    ..My Documents\Visual Studio Samples\LinqSamples\

    ExpressionTreeVisualizer\ExpressionTreeVisualizer

     

    ..My Documents\Visual Studio Samples\LinqSamples

    \QueryVisualizer\SqlServerQueryVisualizer

     

    If you open the *.proj from these two folders and compile them you will get two dlls

    Ø       ExpressionTreeVisualizer.dll

    Ø       LinqToSqlQueryVisualizer.dll

     

    Under bin\debug or bin\release folder depending on the type of build you select. Now copy both the dlls and drop them to the folder located under My Documents

     

    ..My Documents\Visual Studio 2008\Visualizers

     

    Once it is there, and you are in the debug mode inside your visual studio 2008 these two options will come respectively.

     

    View of LinqToSqlQuery Viauslizer

     

    ExpressionTree

     

    Now click on the "Linq to SQL Debugger Visualizer" button,

     

    Sql

     

     

     

     

    View of ExpressionTree Viauslizer

     

    Sql

     

    Now click on the "Expression Tree Visualizer" button,

     

    Sql

     

     

     

     

    Namoskar!!!

  • Wriju's BLOG

    Visual Studio 2008 Videos

    • 2 Comments

    No better place to learn VS 2008 than http://msdn2.microsoft.com/en-us/vstudio/bb655906.aspx 

    Namoskar!!!

  • Wriju's BLOG

    Get 1 GB Storage at Skydrive Windows Live

    • 1 Comments

    Get 1 GB storage online at http://skydrive.live.com. Microsoft launched it today.

    For more details please visit http://skydriveteam.spaces.live.com/default.aspx.

    Namoskar!!!

  • Wriju's BLOG

    LINQ to SQL : Change in DataContext from Beta 1 to Beta 2

    • 2 Comments

    During the demonstration I have noticed that two major methods of DataContext has changed from Beta 1 to Beta 2.

     

    First one, is the new name for the class DataShape(). Now there is no class called DataShape() but we have DataLoadOptions().

     

     

    var loadOption = new DataLoadOptions();           

    loadOption.LoadWith<Customer>(c => c.Orders);

     

    db.LoadOptions = loadOption;

     

    Secondly,

     

    DataContext does not have any method called GetQueryText() it got changed to GetCommand().

     

    Please let me know if you find something else. I will update this entry.

     

    Namoskar!!!

  • Wriju's BLOG

    Pattern’s and Practices Guidance Explorer

    • 2 Comments

    This is one tool which will help you to find anything and everything for what you might scratch your head.

     

    This tool is amazing. Please go ahead and install from

    Download

    Download GuidanceExplorer build 20060926 http://www.codeplex.com/guidanceExplorer/Project/FileDownload.aspx?DownloadId=2716

     

    This is the best Guidance you can get in your day to day programming.

     

    Namoskar!!!

Page 1 of 1 (9 items)