Thursday, December 9, 2010

C# Nullables

                With the introduction of LINQ to SQL it has become inevitable to support the basic feature of  SQL server database that would help us convert queries to LINQ. It was a pain in the neck to deal with the DB null values since the value types such as Int, Decimal, DateTime etc cannot be assigned null. With the introduction of Nullable<T> life is more easier now. .NET also introduced a very user friendly semantic to represent the Nullable<T>  which is T?. Thus,

Nullable<int> and  int? are one and the same
Nullable<decimal> and  decimal? are the same

int? Usage:
Nullable variable can be declared as,
int? i = null;
My personally recommendation would be to use the default keyword to assign null like,
int? i = default(int?);
This will come in handy while using Nullable in Generics

The assignment operation will be just like old styled
    {
        int? i = default(int?);
        i = 100;
       
        int j;
        if (i == null)
            j = 0;
        else
            j = i.Value;

        (OR)

        int j = i == null? 0 : i.Value;
    }

?? Operator:
                If you are little familiar with TSQL you must have heard of coalesce. ?? is the C# equivalent to coalesce in SQL Server and it was made possible because of the Nullable<T> struct.  With these basics in place we can very easily convert datatable to custom objects using LINQ without much hassle.
Sample Implementation of LINQ query to with this operator usage is below.
    public class CustomObject
    {
        public int? Field1;
        public decimal? Field2;
    }

var cos = (from table1Row in table1.AsEnumerable()
                join table2Row in table2.AsEnumerable()
                on table1Row.Field<int>("JoinID") equals table2Row.Field<int>("JoinID")
                select new CustomObject
                {
  //Usage of Nullable struct
                    Field1 = table1Row.Field<int?>("ID"),
                    //Usage of coalesce operator
                    Field2 = table1Row.Field<decimal?>("Price") ?? table2Row.Field<decimal?>("Price")
                }).ToList<CustomObject>();

Alas we now the object with null values and right price without much hassle.

Summary
We have just discovered a new way to create objects in .NET that can closely represent the data value from SQL server. With the introduction of Nullable<T> and coalesce operator, life should be much easier and cleaner in handling the null values from the Database for .NET value types. However this is just a tip of the iceberg that I tasted. Hope you will see these in action with much complicated scenario and will appreciate the real value of it. Happy Coding.
Reference:  http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. With the introduction of LINQ to SQL it has become inevitable to support the basic feature of SQL server database that would help us convert queries to LINQ. It was a pain in the neck to deal with the DB null values since the value types such as Int, Decimal, DateTime etc cannot be assigned null.Thanks for your explanation. dot net training in velachery | dot net training in chennai

    ReplyDelete