1
leaves = leaves.Where(s => s.Employee.Name.ToUpper().Contains(searchString.ToUpper())
                                   || s.StartDate.ToString().Contains(searchString));     

This will leads to an error..please help me,

Error: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression..

2 Answers 2

3

Treat each case differently

DateTime dateValue;

if (DateTime.TryParse(searchString, out dateValue))
{
    leaves = leaves.Where(l => l.StartDate == dateValue);
}
else
{
    leaves = leaves.Where(s => s.Employee.Name.ToUpper().Contains(searchString.ToUpper()));
}
Sign up to request clarification or add additional context in comments.

2 Comments

it works fine just missing this, leaves = leaves.Where(s => s.Employee.Name.ToUpper().Contains(searchString.ToUpper()));
@user1134291 Sorry about that missing part. Updated my answer.
1

The error message says exactly what is going on: Entity Framework cannot translate that part of the expression into a SQL statement, which means you cannot search this way.

Without more context, your best option is probably to search by real dates (i.e. search since or until a certain date).

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.