Pranay Rana: November 2012

Monday, November 26, 2012

Linq Query to compare only Date part of DateTime

In this post I am going to discuss about comparing the date part of the datetime in linq query. In linq query its easy to perform datetime comparison how to compare only date not time part of datetime filed of your entity.

Let have coloseure look, In Sql Server one can do something like as below to just compare date part of the field.
SELECT * FROM dbo.tbl_MyTable
WHERE 
CAST(CONVERT(CHAR(10), DateTimeValueColumn, 102) AS DATE) = 
            CAST(CONVERT(CHAR(10),GETDATE(),102) AS DATE)
So in above query convert function did task of removing time part and only date part comparison happens.

Linq Queires
Following discussion about doing same task using linq queries.
Solution 1:
First way to achieve same thing (i.e. comparing date part) of entity or object is following

var data = context.t_quoted_value.Where(x => x.region_name == "Hong Kong" 
                  && DateTime.Compare(x.price_date.Value.Date, dt.Date) == 0)
                  .ToList();

Here Date property of DatTime used to get only date part of datetime property and made use of DateTime.Compare function to get matching object.

But the problem with this approach when make use of EnityFramework i.e. Linq To Entity , its gives following error at runtime

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

so to avoid above error in Linq to Entity query is modified to following

var data = context.t_quoted_value.Where(x => x.region_name == "Hong Kong")
                            .ToList()
                            .Where (x=> DateTime.Compare(x.price_date.Value.Date, dt.Date) == 0)
                            .ToList(); 

So in above query first data is get fetched from the database and than on list date comparison get applied. But the problem with this approach is need to load all data first than the date comparison get applied because entityframework doesn't support direct query.

Solution 2:
One more easy and simple solution to just compare datet part of datetime object is as following
            
var data1 = context.t_quoted_value.Where(x => x.region_name == "Hong Kong" 
                            && x.price_date.Value.Year == dt.Year
                            && x.price_date.Value.Month == dt.Month
                            && x.price_date.Value.Day == dt.Day).ToList();
its query use the year,month and day property of datetime object to compare date. Advantage of this solution is this is compatible with all flavor of Linq i.e. it works in linq to sql, linq to object and also in linq to enitity.

Conclusion
So the post is useful when you need to compare only date part of datetime property in Linq queries.

Leave your comments if you like it.

Wednesday, November 7, 2012

Read Xml with Descendants Method (XName)

This post is about understanding Descendants and avoid misconception with this method.
Recently I read one question on StackOverFlow  about reading xml using Linq To Xml to get node values. In that developer made use of Descendants Method to get the child node values.

Let see the actual problem here following is XML to read. And developer wrote code to read out value of orderid node.
<ordersreport date="2012-08-01">
<returns>
      <amount>
        <orderid>2</orderid>
        <orderid>3</orderid>
        <orderid>21</orderid>
        <orderid>23</orderid>
      </amount>
    </returns>
</ordersreport>
So code written like this
    var amount = documentRoot.Descendants("Amount")
               .Select(y => new
               {
                  OrderId = (int)y.Element("OrderId")
               });
               foreach (var r in amount)
               {
                  Console.WriteLine(r.OrderId);
               }
Ouput of above code is
 2
that is only first orderid element value which is child of Amount , So misconception here by developer of the code is Descendants("Amount") returns child element of the Amount tag i.e. all orderId element.

Now to Understand Descendants function in better way I visited to MSDN link which says something like this
XContainer.Descendants Method (XName) - Returns a filtered collection of the descendant elements for this document or element, in document order. Only elements that have a matching XName are included in the collection. So as per the defincation on MSDN problem with code
    var amount = doc.Descendants("Amount")                         
      .Select(y => new
      {
       OrderId = (int)y.Element("OrderId")
       });
will give you Element Amount and when you write y.Element("OrderId") will return you fist element of its child.
Descendants - doesn't mean than its return the child element of element name rather than method look for descendants of element or if name of elemnt specified as parameter than matching descendants.
Finally I got following solution to get it properly work
XElement documentRoot  = 
     XElement.Parse (@"<ordersreport date="2012-08-01">
                             <returns>
                              <amount>
                                  <orderid>2</orderid>                                                    
                                  <orderid>3</orderid>
                                  <orderid>21</orderid>
                                  <orderid>23</orderid>
                               </amount>
                             </returns>
                        </ordersreport>");
Solution 1
var orderids = from order in
                  documentRoot.Descendants("Amount").Descendants()
                  select new
                  {
                     OrderId = order.Value
                  };
As per the information on MSDN documentRoot.Descendants("Amount").Descendants() give list of orderId elements.

Solution 2
var orderids = from order in
                    documentRoot.Descendants("OrderId")
                    select new
                    {
                       OrderId = order.Value
                    };
or the second solution is just bit easy than this just make use of documentRoot.Descendants("OrderId") that will give all orderidelement.

Conclusion
This post is just for avoiding misconception related to Descendants and understand it properly.

Leave your comments if you like it.