user1537319
user1537319

Reputation:

Notsupportedexception was unhandled by user code

My code:

i f(!string.IsNullOrWhiteSpace(gender))
    if (gender == "NULL")
        predicate = predicate.And(x => string.IsNullOrWhiteSpace(gender));
    else
        predicate = predicate.And(x => x.Gender == gender);

When gender is NULL and when I am executing the flowing line:

var filteredUsers = _personExtendedRepository.GetMany(predicate).ToList();

an error occurs:

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

Note: When I am executing the following line in SQL Server Management Studio:

SELECT * FROM UVW_Sample WHERE Gender IS NULL

Records are displaying. Please help how to solve this issue.

Upvotes: 1

Views: 2289

Answers (2)

zmbq
zmbq

Reputation: 39023

LINQ-to-Entities is limited in what it can do, as it translates your expression to SQL, and it doesn't know how to translate string.IsNullOrWhiteSpace to SQL. It also doesn't know how to translate .ToString() to SQL.

What you need to do is perform the translation outside LINQ-to-Entities. In your case, your predicate should be:

x=>x==null || x.Trim()==""

Upvotes: 2

Andrei
Andrei

Reputation: 56716

string.IsNullOrWhiteSpace cannot be translated into SQL, so if you want to check whether column is null use something like:

predicate = predicate.And(x => x.Gender == null);

Upvotes: 1

Related Questions