i got this exception in .net when try to compare date in linq

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();

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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();

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x