[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2014-07-15
Medium Priority
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 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 40198463
Your problem is that the parameter:

...is 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

872 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