LinqToEntities question

  • Thread starter Thread starter Trebor
  • Start date Start date
T

Trebor

Hi,

I'm trying to convert a code from 'linq to sql' to 'linq to entities' and
I'm facing the problem when I'm trying to substract two datetime columns.

var q = from l in lh
where (l.LoginDate >= Date1)
select new
{
l.UserId,
l.LoginDate,
l.LogOutDate,
Minutes =
(DateTime?)System.DateTime.Today.Add((TimeSpan)( l.LogOutDate -
l.LoginDate )) // this line causes a runtime error in 'Linq to Entities'
};


The error message is: LINQ to Entities does not recognize the method
'System.DateTime Add(System.TimeSpan)' method,
and this method cannot be translated into
a store expression.


Any help will be appreciated.
 
Trebor said:
Hi,

I'm trying to convert a code from 'linq to sql' to 'linq to entities'
and I'm facing the problem when I'm trying to substract two datetime
columns.

var q = from l in lh
where (l.LoginDate >= Date1)
select new
{
l.UserId,
l.LoginDate,
l.LogOutDate,
Minutes =
(DateTime?)System.DateTime.Today.Add((TimeSpan)( l.LogOutDate -
l.LoginDate )) // this line causes a runtime error in 'Linq to Entities'
};


The error message is: LINQ to Entities does not recognize the method
'System.DateTime Add(System.TimeSpan)' method,
and this method cannot be translated
into a store expression.

You have to add a custom function mapping for DateTime.Add()

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
Back
Top