簡體   English   中英

Notsupportedexception未被用戶代碼處理

[英]Notsupportedexception was unhandled by user code

我的代碼:

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

當性別為NULL並且我執行流線時:

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

發生錯誤:

“LINQ to Entities無法識別方法'Boolean IsNullOrWhiteSpace(System.String)'方法,並且此方法無法轉換為商店表達式。”

注意 :當我在SQL Server Management Studio中執行以下行時:

SELECT * FROM UVW_Sample WHERE Gender IS NULL

記錄正在顯示。 請幫忙解決這個問題。

LINQ-to-Entities的功能有限,因為它將表達式轉換為SQL,並且不知道如何將string.IsNullOrWhiteSpace轉換為SQL。 它也不知道如何將.ToString()轉換為SQL。

您需要做的是在LINQ-to-Entities之外執行翻譯。 在您的情況下,您的謂詞應該是:

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