Monday, February 27, 2012

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

Apparently, Entity Framework doesn't allow the use of the Date member of DateTime inside an Entity Framework LINQ Query. The following query fails because of the use of .Date

var salesResult = (from sale in T_Sales
            where ((DateTime)sale.Sale_Date).Date == DateTime.Today
            select sale).ToList();


The above code will raise the following exception:



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





I found out form this link that there are EntityFunctions available. So the above code can be replaced with the code below to work:


var salesResult = (from sale in T_Sales
            where EntityFunctions.TruncateTime((DateTime)sale.Sale_Date) == DateTime.Today
            select sale).ToList();




However checking the details of this EntityFunctions on the MSDN site I found out that it is only available to Framework 4.0 and 4.5.

Wednesday, November 23, 2011

How to use CompareValidator to validate dates in different format (e.g., dd/MM/yyyy)?

If you want to use the ASP.NET CompareValidator control to validate dates in a different format, you can setup the Page to your desired culture. You can do this by either setting up the value in the page directive as shown below:

<%@ Page culture="your culture here" %>

Or by setting it up in the page load event:


protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
     Page.Culture = "your culture here";
   }
}

For example if you want to validate a date in Australian Date Format, which is dd/MM/yyyy, you can do:

<%@ Page culture="en-AU" %>

Or: 



protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
     Page.Culture = "en-AU";
   }
}

Thursday, November 17, 2011

Hello World!

I'm starting up this blog to compile all of the things that I want to remember. Most of these posts are solutions and how to's to the problems I've encountered in web development and programming in general.