DateTime datatype column syntax in a entity model query in ASP.NET MVC

Posted on 2014-07-15
Last Modified: 2014-07-16
I'm working on an ASP.NET mvc application.
I'm using Visual Studio 2013.
I created a Entity Model of the Employees table of the Northwind sql server database.
The name of my model is EmployeesMode.edmx.

In the Models folder along with the model I have a class called ModelService.cs which holds the queries I am going to us in my WebGrid filter.

My question is in regards to this snippet:

        private static IQueryable<Employee> filterPeople(int? employeeID, string lastName, string firstName, string title, string titleOfcourtesy, DateTime? birthDate, string address, string city, string region, string postalCode, string country, string homePhone, IQueryable<Employee> query)
            // EmployeeID
            if (employeeID != null)
                query = query.Where(p => p.EmployeeID == employeeID);
            // LastName
            if (!string.IsNullOrWhiteSpace(lastName))
                query = query.Where(p => p.LastName.Contains(lastName));
            // FirstName
            if (!string.IsNullOrWhiteSpace(firstName))
                query = query.Where(p => p.FirstName.Contains(firstName));
            // Title
            if (!string.IsNullOrWhiteSpace(title))
                query = query.Where(p => p.Title.Contains(title));
            // TitleOfCourtesy
            if (!string.IsNullOrWhiteSpace(titleOfcourtesy))
                query = query.Where(p => p.TitleOfCourtesy.Contains(titleOfcourtesy));
            // BirthDate
            if (!string.IsNullOrWhiteSpace(birthDate))
                query = query.Where(p => p.BirthDate.ToString().Contains(birthDate));

            // Address
            if (!string.IsNullOrWhiteSpace(address))
                query = query.Where(p => p.Address.Contains(address));
            // City
            if (!string.IsNullOrWhiteSpace(city))
                query = query.Where(p => p.City.Contains(city));
            // Region
            if (!string.IsNullOrWhiteSpace(region))
                query = query.Where(p => p.City.Contains(region));
            // PostalCode
            if (!string.IsNullOrWhiteSpace(postalCode))
                query = query.Where(p => p.PostalCode.Contains(postalCode));
            // Country
            if (!string.IsNullOrWhiteSpace(country))
                query = query.Where(p => p.Country.Contains(country));
            // HomePhone
            if (!string.IsNullOrWhiteSpace(homePhone))
                query = query.Where(p => p.HomePhone.Contains(homePhone));

            return query;

Open in new window

i'm getting an error on the birthDate column because its a DateTime data time.

This pic shows I'm not declaring this datetime column correctly
my error
Anyone know the syntax to declare a DateTime column?
Question by:maqskywalker
    1 Comment
    LVL 74

    Accepted Solution

    Your problem is that the parameter:

    Snippet declared as a nullable DateTime, but you are trying to use it like a string. The if clause should be:

    if (birthDate.HasValue)

    Open in new window

    ...and I suppose the body would be:

    query = query.Where(p => p.BirthDate == birthDate)

    Open in new window


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now