LinqToEntities question

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

Frans Bouma [C# MVP]

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#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top