# Linq To Entities DBFunctions and SqlFunctions

When using linq to sql queries,few functions like DateTime Comparison,FirstorDefault would not work and you would end up getting exception “LinqtoEntities is not supported by this function”.

Problem:

For instance I was trying to compare a column of Date-time type with today’s date,I had issue when comparing it since the sql server Date-Time type was of (yyyy-mm-dd) and today’s date was of (mm-dd-yyyy),I was not able to use ToString method to format date since linq to entities  did not support it.

Solution:

LinqToEntities provides DBFunction and SqlFunction for sql related operations.It has got several methods like DateAdd,DatePart,TruncateTime etc…I had used the same to compare dates.


var somequery = from f in mytable where DbFunction.TruncateTime (f.mydatecolumn) == Dbfunction.TruncateTime(SqlFunction.GetDate())

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s