h1

AddDays in a LINQ Lambda Expression

January 25, 2012

Using Entity Framework 4, we were trying to run a lambda expression that added an integer field from one table to a DateTime field in another related table.

.FindMany<Object1>(object1=> object1.DateCompleted.AddDays(object1.object2.NumberOfDays)  <= System.DateTime.Now)

Got this error message:

LINQ entities does not recognize the method System.DateTime.AddDays Cannot be translated into a stored expression

What we had to do was use the System.Data.Objects.EntityFunctions.AddDays function instead.  See Entity Functions - Add Days

.FindMany<Object1>(object1=> System.Data.Objects.EntityFunctions.AddDays(object1.DateCompleted, object1.objecto2.NumberOfDays)  <= System.DateTime.Now)

Advertisement

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 )

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

Follow

Get every new post delivered to your Inbox.