-1

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

var tripss = context.trips.Join(context.UserMasters, t => t.DriverId, u => u.UserID,
                (tr, us) => new
                {
                    DateOfTrip = DbFunctions.TruncateTime(tr.DateOfTrip),
                    DriverId = tr.DriverId,
                    FromCity = tr.FromCity,
                    ID = tr.ID,
                    PlaceToMeet = tr.PlaceToMeet,
                    TimeOfTrip = tr.TimeOfTrip,
                    ToCity = tr.ToCity,
                    Name = (us.FullName == null) ? us.UserName : us.FullName,
                    ImageUrl = us.ImageUrl,
                    PostTime=(DbFunctions.TruncateTime( tr.TimeOfPost)==DateTime.Now.Date)?
                    (tr.TimeOfPost.Value.Hour==DateTime.Now.Hour)? (DbFunctions.DiffMinutes(DateTime.Now,tr.TimeOfPost).Value)
                    : DbFunctions.DiffHours(DateTime.Now,tr.TimeOfPost).Value : DbFunctions.DiffDays(DateTime.Now, tr.TimeOfPost).Value


                }).ToList();
  • Check this answer - https://stackoverflow.com/questions/6919709/only-initializers-entity-members-and-entity-navigation-properties-are-supporte – MBB Aug 09 '20 at 18:04
  • Does this answer your question? [Only initializers, entity members, and entity navigation properties are supported](https://stackoverflow.com/questions/6919709/only-initializers-entity-members-and-entity-navigation-properties-are-supporte) – Timothy G. Aug 09 '20 at 19:07
  • @TimothyG. no it doesn't, the answer is in this code {DbFunctions.TruncateTime( tr.TimeOfPost)==DateTime.Now.Date)} when using date time in linq expressions we cant use date property in datetime class – Ahmed Zakaria Aug 09 '20 at 19:50

2 Answers2

1

the answer is in this code DbFunctions.TruncateTime( tr.TimeOfPost)==DateTime.Now.Date) when using datetime.now in linq expressions we cant use date property in datetime class removing .date solved the problem

0

For this query you can move the date time variables outside the query.

var dt =    DateTime.Now.Date;
var now = DateTime.Now;
var hour = DateTime.Now.Hour;



    var tripss = context.trips.Join(context.UserMasters, t => t.DriverId, u => u.UserID,
                    (tr, us) => new
                    {
                        DateOfTrip = DbFunctions.TruncateTime(tr.DateOfTrip),
                        DriverId = tr.DriverId,
                        FromCity = tr.FromCity,
                        ID = tr.ID,
                        PlaceToMeet = tr.PlaceToMeet,
                        TimeOfTrip = tr.TimeOfTrip,
                        ToCity = tr.ToCity,
                        Name = (us.FullName == null) ? us.UserName : us.FullName,
                        ImageUrl = us.ImageUrl,
                        PostTime=(DbFunctions.TruncateTime( tr.TimeOfPost)== dt)?
                        (tr.TimeOfPost.Value.Hour== hour)? (DbFunctions.DiffMinutes(now, tr.TimeOfPost).Value)
                        : DbFunctions.DiffHours(now, tr.TimeOfPost).Value : DbFunctions.DiffDays(now, tr.TimeOfPost).Value
    
    
                    }).ToList();
Dawood Awan
  • 7,051
  • 10
  • 56
  • 119