1

i have a problem in in Linq to entity when selecting this is my code
i want to select with date and id

    Login Lo = new Login();
    DateTime CurrentDate = DateTime.Now;         
    Lo = db.Logins.Where(i => i.Emp_id == employee.id &&
     DbFunctions.TruncateTime(i.WorkDay).Value.Date == DbFunctions.TruncateTime(CurrentDate).Value).FirstOrDefault();

it give me this Error :

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

3
  • please see this q&a for the same issue stackoverflow.com/questions/11597373/… Commented Aug 24, 2016 at 18:08
  • LINQ to Entities basically translates your expression in to a SQL statement. It doesn't know how to convert Date operations. You'll need to create them as string comparisons. Commented Aug 24, 2016 at 18:08
  • Thanks for helping i found the solution Commented Aug 24, 2016 at 18:22

1 Answer 1

2

To solve your problem you could do this:

DateTime CurrentDate = DateTime.Now.Date;   
var result= db.Logins.Where(i => i.Emp_id == employee.id &&
                                 DbFunctions.TruncateTime(i.WorkDay) == currentDate).FirstOrDefault();

Your issue is because the Linq to Entities provider can't translate the operations that are defined in the Date property to a proper expression in SQL:

    // Returns the date part of this DateTime. The resulting value
    // corresponds to this DateTime with the time-of-day part set to
    // zero (midnight).
    //
    public DateTime Date {
        get { 
            Int64 ticks = InternalTicks;
            return new DateTime((UInt64)(ticks - ticks % TicksPerDay) | InternalKind);
        }
    }

That's why my advice is, outside of your query, call Date property from DateTime.Now, this way you will get the current date without the time part, and in your query, you don't need to call Date property, that's the function of DbFunctions.TruncateTime method

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.