none
Date field comparison in linq DataService Query RRS feed

  • Question

  • I have a WCF data service to fetch the customer records from a table which has Datetime column as well. In my UI I have a grid which displays Name and DOB.I have a text box for filter. When there is a text entered in the filter, I want that text to be compared against all the columns in the table. I am getting an exception when the search text is compared against the Datetime column. This is the query am using below.

    enter code here
    db.Customers.Where(customer => customer.LastName.ToUpper().StartsWith(text) ||
                                   customer.FirstNme.ToUpper().StartsWith(text) ||
                                   (customer.DOB.Value.Year.ToString()
                                                           .StartsWith(text)
                      );

    This throws an exception 'Error translating the linq expression to URI'.

    On the other hand if I just compare the Datetime field with a direct value it works.

    enter code here
    
    db.Customers.Where(customer => customer.LastName.ToUpper().StartsWith(text) ||
                       customer.FirstNme.ToUpper().StartsWith(text) ||
                       (customer.DOB.Value.Year == 1985)
                      );

    How can I compare my Datetime column with the entered text??


    Hari

    Friday, March 21, 2014 3:37 PM

Answers