10

var islemList = (from isl in entities.Islemler where (isl.KayitTarihi.Date >= dbas && isl.KayitTarihi.Value.Date <= dbit) select isl);

It gives error: date is not supported in LINQ to Entities... How can i get date in linq.

Hinek
  • 9,519
  • 12
  • 52
  • 74
serkan
  • 109
  • 1
  • 3

3 Answers3

30

Use EntityFunctions.TruncateTime.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • 12
    Note that EntityFunctions is now deprecated and System.Data.Entity.DbFunctions should be used instead. – Meryovi Jun 07 '14 at 15:25
  • 2
    *Now* being EF 6 onwards. The implementations haven't changed, it's simply a rename with the old functions delegating to the new functions, so you can safely update your existing code. From the commit message: *"The classes with the old names basically just delegate to the new classes and are also marked as obsolete to help move people to the new classes."* – user247702 Aug 26 '14 at 08:24
  • 1
    EntityFunctions.TruncateTime is now deprecated, instead use DbFunctions.TruncateTime – Paul Zahra Jun 03 '20 at 15:54
1

if KayitTarihi is a date column in DB (and dbas and dbit are DateTime), use:

var islemList = (from isl in entities.Islemler where (isl.KayitTarihi >= dbas && isl.KayitTarihi <= dbit) select isl);
Jaroslav Jandek
  • 9,463
  • 1
  • 28
  • 30
  • 1
    KayitTarihi is DateTime but i need just Date – serkan Jul 06 '10 at 11:07
  • 1
    Try `((Nullable)isl.KayitTarihi.Date)`. Not sure how si LINQ going to react. You could alternatively modify `dbas` and `dbit` (if they are without the time part) so you do not have to extract Date value from `KayitTarihi`: `...where (isl.KayitTarihi >= dbas && isl.KayitTarihi < dbit.AddDays(1))...`. – Jaroslav Jandek Jul 06 '10 at 12:37
-2

The .Date property is not supported in Linq to Entities (though it may be supported in other implementations of Linq).

I realize that you only want to compare the dates, but there is no real problem with comparing the datetimes if the dbas and dbit values are datetimes with time 00:00:00. You might have to offset the dates or use other inequality checks to get the proper interval but the comparison will work as you intend.

I would personally go with Jandek's solution.