简体   繁体   中英

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.

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()==""

string.IsNullOrWhiteSpace无法转换为SQL,因此如果要检查列是否为null,请使用以下内容:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM