Luca Romagnoli
Luca Romagnoli

Reputation: 12455

compare the dates with linq

I have this code:

   i.SpesaVitto = db.TDP_NotaSpeseSezB.Where(p => p.GiornoFine == null && 
                ((DateTime)p.Giorno).Date == ((DateTime)i.Data).Date &&
                p.MissioneID == missioneID).Sum(p => p.Costo);

If i launch it i obtain this error:

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

how can i compare the datetimes field without the hours part

thanks

Upvotes: 0

Views: 1571

Answers (3)

torpederos
torpederos

Reputation: 841

solution for your problem will be the "SqlFunctions class and DateDiff" http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.aspx

Upvotes: 0

NebuSoft
NebuSoft

Reputation: 4000

Well this is certainly not the most efficient way to do it, but you can try something like:

i.SpesaVitto = db.TDP_NotaSpeseSezB.Where(p => p.GiornoFine == null && 
            (new DateTime((p.Giorno as DateTime).Year, (p.Giorno as DateTime).Month, (p.Giorno as DateTime).Day) == (new DateTime((i.Data as DateTime).Year, (i.Data as DateTime).Month, (i.Data as DateTime).Day) &&
            p.MissioneID == missioneID).Sum(p => p.Costo);

Depending on the requirements I would probably implement something cleaner and faster but it would depend on how the dates are stored in your database.

Upvotes: 1

Jim Counts
Jim Counts

Reputation: 12795

Check out this question it looks like the syntax error may be in your SQL, not in your LINQ. The asker in that question had written:

SQL ------
Select
 EmployeeNameColumn
From
EmployeeTable
WHERE StartDateColumn.Date <= GETDATE() //Today
SQL ------

When they should have written:

SQL ------
Select
 EmployeeNameColumn
From
EmployeeTable
WHERE StartDateColumn <= GETDATE() //Today
SQL -

Upvotes: 0

Related Questions