简体   繁体   English

Notsupportedexception未被用户代码处理

[英]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: 当性别为NULL并且我执行流线时:

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." “LINQ to Entities无法识别方法'Boolean IsNullOrWhiteSpace(System.String)'方法,并且此方法无法转换为商店表达式。”

Note : When I am executing the following line in SQL Server Management Studio: 注意 :当我在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. LINQ-to-Entities的功能有限,因为它将表达式转换为SQL,并且不知道如何将string.IsNullOrWhiteSpace转换为SQL。 It also doesn't know how to translate .ToString() to SQL. 它也不知道如何将.ToString()转换为SQL。

What you need to do is perform the translation outside LINQ-to-Entities. 您需要做的是在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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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