February, 2009

  • Wriju's BLOG

    C# 4.0 : New Extension Method “Zip”

    • 3 Comments

    While browsing the features I found an interesting extension method called “Zip”.

    How it works, let’s check it.

    List<string> names = new List<string>

    {"Debajyoti", "Sumitra", "Wriju" , "Writam", "Tupur" };

     

    List<int> ages = new List<int>{ 67, 58, 30, 24, 26 };

     

    var zipped = names.Zip(ages, (name, age) => name + " is " + age + " years old.");

     

    foreach (var item in zipped)

    {

       Console.WriteLine(item);

    }

     

    Will give you the below output,

     

    Debajyoti is 67 years old.

    Sumitra is 58 years old.

    Wriju is 30 years old.

    Writam is 24 years old.

    Tupur is 26 years old.

     

     

    I love B#.

    Namoskar!!!

  • Wriju's BLOG

    C# 4.0 : Named Arguments

    • 3 Comments

    For methods having numerous arguments we tend to get confused. And this is also pain for others while reviewing the code. C# 4.0 gives us an opportunity to pass parameters with name.

    How it works, let’s check it. Suppose you have a method like below,

    public static class TestClass

    {

       public static void ParamMethod(string Name, double Salary,

    string Department, int Age = 30)

       {

           Console.WriteLine("Name = {0}", Name);

           Console.WriteLine("Age = {0}", Age);

           Console.WriteLine("Salary = {0}", Salary);

           Console.WriteLine("Department = {0}", Department);           

       }

    }

     

    Now the calling would look like,

     

    TestClass.ParamMethod(Name:"Wriju", Salary:1000.00,

       Department:"Software",Age:31);

     

    You can alter the sequence of the parameters too,

     

    TestClass.ParamMethod("Tupur", 2000.00,

       Department: "IT", Age: 26);

    No compiler overhead is associated with it.

    Rule, Non-optional parameters has to be specified either sequentially or by name.

    Namoskar!!!

  • Wriju's BLOG

    C# 4.0 : Optional Parameters

    • 2 Comments

    Days of creating multiple overloads to avoid parameters are gone. You no more have to create many methods and pass the default one there. C# 4.0 brings to us the concept of “optional” parameters.

    How it works, let’s check it. Suppose you have a method like below,

    public static class TestClass

    {

       public static void ParamMethod(string Name, double Salary,

    string Department, int Age = 30)

       {

           Console.WriteLine("Name = {0}", Name);

           Console.WriteLine("Age = {0}", Age);

           Console.WriteLine("Salary = {0}", Salary);

           Console.WriteLine("Department = {0}", Department);           

       }

    }

     

    Here you have mentioned the default value using “=” for the parameter “Age”.

     

    Rule!!! You always have to put optional parameters after the “non-optional” parameters.

     

    Now if you write the below code and call the method ParamMethod, you can omit the “optional parameter” Age.

     

     

    class Program

    {

       static void Main(string[] args)

       {

           TestClass.ParamMethod("Wriju", 1000.00, "Software");

       }

    }

    Will give you the below output,

     

    Name = Wriju

    Age = 30

    Salary = 1000

    Department = Software

     

     

    Namoskar!!!

  • Wriju's BLOG

    C# 4.0 : Dynamic Objects

    • 2 Comments

    “Working with dynamic objects statically” – Anders Hejlsberg

    When you work with C# 4.0 you get to work with many unmanaged world “dynamically”. What does that mean to you? It means you do not do things “statically” as you used to do in C# or in VB.NET. There are many benefits in static programming like statement completion, compile time checking, refactoring etc. But when it comes to interact with other types (those are unmanaged) you have to take chances by creating wrapper or call the native methods in parameters (by calling “Invoke” method and pass the name in string).

    What is the benefit?

    This will extend the power of static programming for any dynamic programming language like Iron Python or Ruby, even Silverlight or JavaScript.

    In C# 4.0 you can statically type the dynamic objects. How that happens,

    When you say ,

    int i = 10;

    var j = 11;

    You are statically mentioning that j is of type int. And this gives you intellisense and you cannot do things even at the design time which are not allowed. This will even prevent you to build the assembly.

    But when you say something like this,

    dynamic i = 10;

    dynamic d = 12.9;

    dynamic s = "Hello";

    it means you are “statically declaring dynamic” objects. What does that really mean?

    Ø  The objects will be deferred at run-time (not in compile time, unlike “var”)

    Ø  At the runtime all of them will be substituted for dynamic

    Ø  Any result of any dynamic operation is again “dynamic”.

    Now if we write the below block to check-out what is happening around,

    static void Main(string[] args)

    {

       dynamic i = 10;

       dynamic d = 12.9;

       dynamic s = "Hello";

     

       Console.WriteLine(i.GetType());

       Console.WriteLine(d.GetType());

       Console.WriteLine(s.GetType());

    }

    This will give you the below output,

     

    System.Int32

    System.Double

    System.String

     

    Below I have created a class called “DynaClass” and it has got few overloaded methods with different types of parameters. Here I will demonstrate how the run-time resolution would happen for the keyword “dynamic”.

    public static class DynaClass

    {

    public static string CallMe(string i)

    {

              return "You have called method for String";

    }

     

    public static string CallMe(int i)

    {

              return "You have called method for Int";

    }

     

         public static string CallMe(double i)

    {

              return "You have called method for Double";

    }

     

    public static string CallMe(DateTime i)

    {

    return "You have called method for DateTime";

    }

    }

      

    Now if you call the method through this code block,

    //Call for Int

    dynamic x1 = 10;

    Console.WriteLine(DynaClass.CallMe(x1));

     

    //Call for Double

    dynamic x2 = DateTime.Now;

    Console.WriteLine(DynaClass.CallMe(x2));

     

    //Call for String

    dynamic x3 = "Hello Dynamic";

    Console.WriteLine(DynaClass.CallMe(x3));

     

    //Call for DateTime

    dynamic x4 = DateTime.Now;

    Console.WriteLine(DynaClass.CallMe(x4));

    Will give you the below output,

     

    You have called method for Int

    You have called method for DateTime

    You have called method for String

    You have called method for DateTime

     

    As I said earlier that dynamically called method returns value dynamically. So the method CallMe will give you dynamic not “string” which will be resolved at run-time.

    Namoskar!!!

  • Wriju's BLOG

    Azure Resources : How Do I Do Series Vides

    • 1 Comments

    Check out this url http://msdn.microsoft.com/en-us/azure/dd439432.aspx 

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET Entity Framework : Working with Stored Procedure

    • 3 Comments

    Here we go, I generally get questions from developers that if they will have to scrub the existing powerful stored procedure they have written by investing time and money. The answer to that is “NO”. You must enjoy using it as it has its own power.

     

    So it is very simple in ADO.NET Entity Framework. Little bit of background before we start,

     

    We need a table

     

    CREATE TABLE [dbo].[Emp](

          [Id] [int] IDENTITY(1,1) NOT NULL,

          [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

     CONSTRAINT [PK_Emp] PRIMARY KEY CLUSTERED

    (

          [Id] ASC

    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

    ) ON [PRIMARY]

     

    There will be three stored procedures, insert/update/delete

     

    Insert

    ++++

    ALTER PROCEDURE [dbo].[InsertEmp]  

    (

                @Name VARCHAR(50)

    )    

    AS

    BEGIN

          INSERT INTO Emp(Name) VALUES(@Name)

    END  

     

    Update

    +++++

    ALTER PROCEDURE [dbo].[UpdateEmp]

    (

          @Id int,

          @Name VARCHAR(50)

    )    

    AS

          UPDATE Emp SET [Name] = @Name WHERE ID = @Id

          RETURN

     

    Delete

    ++++

    ALTER PROCEDURE [dbo].[DeleteEmp]

    (

          @Id int

    )

         

    AS

          DELETE Emp WHERE ID = @Id

     

     

    Now the story begins, you create your apps in Visual Studio 2008 (any kind). And add edmx to it. While creating the model select the stored procedures and table you will be working with.

     

    So your final edmx would look like,

     

    image

    EDMX

     

     

    image

    Model Browser

    After that you right click on the edm designer and choose “Mapping Details”. In that you choose “Map Entity to Functions” then put the required stored procedures. You are done.

     

    image

    Stored Procedure mapping

    Now if you are observing the SQL Profiler you would be able to see that the stored procedures are getting executed instead of your T-SQL.

    I am planning to create a screen capture, when ready I will add that to this blog.

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET Entity Framework : Paging EntitySQL

    • 1 Comments

    Paging in EntitySQL is really easy to achieve. You have got two important sub-clauses (or query operators) are available, SKIP and LIMIT.

     

    So your paging EntitySQL query would ideally look like,

    "SELECT VALUE e FROM NorthwindEntities.Customers AS e ORDER BY e.CompanyName SKIP 10 LIMIT 10 ";

     

    The most important part is that you *must* implement ORDER BY clause for SKIP and LIMIT.

     

    using (EntityConnection conn =

        new EntityConnection("name=NorthwindEntities"))

    {

        conn.Open();

        //EntitySQL

        string sQueryString = "SELECT VALUE e FROM NorthwindEntities.Customers AS e ORDER BY e.CompanyName SKIP 10 LIMIT 10 ";

     

        using (EntityCommand comm =

            new EntityCommand(sQueryString,conn))

        {

            EntityDataReader reader =

                comm.ExecuteReader(CommandBehavior.SequentialAccess);

            while (reader.Read())

            {

                Console.WriteLine(reader["CompanyName"]);

            }

        }

    }

     

    PS. You need to make CommandBehavior.SequentialAccess for EntityDataReader.

     

    Namoskar!!!
  • Wriju's BLOG

    ADO.NET Entity Framework : Using EntityClient and EntitySQL

    • 1 Comments

    Using Entity Framework with LINQ is simple and fun stuff. But if you are concerned about the performance and comfortable writing ADO.NET like code then EntityClient is for you. It resides under System.Data.EntityClient namespace. If you are using it you also need to use the EntitySQL (not T-SQL) so the query will be little different. Here is a first look to it,

     

    using (EntityConnection conn =

        new EntityConnection("name=NorthwindEntities"))

    {

        conn.Open();

        //EntitySQL

        string sQueryString = "SELECT VALUE e FROM " +

            " NorthwindEntities.Customers AS e";

     

        using (EntityCommand comm =

            new EntityCommand(sQueryString,conn))

        {

            EntityDataReader reader =

                comm.ExecuteReader(CommandBehavior.SequentialAccess);

            while (reader.Read())

            {

                Console.WriteLine(reader["CompanyName"]);

            }

        }

    }

     

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET and Data Articles by John Papa

    • 0 Comments

    Many of you might be following John Papa’s article on Data and related topics. Myself have came across many of his and are still following. I would like to share a list of articles by him published in MSDN Magazine and available online for you.

    Namoskar!!!

Page 1 of 1 (9 items)