SQL Server 上的 EF Core 3.1 有沒有辦法截斷字串?
我知道有一種方法可以使用DbFunctions.TruncateTimeand來獲取日期和數字DbFunctions.Truncate。
例如,我有一個選擇運算式,我想截斷地址字串:
context.Items.Select(x => new ItemModel
Zipcode = x.Address.Zipcode.Truncate(6)
);
這將引發錯誤:
LINQ to Entities 無法識別方法 'System.String Truncate(System.String, Int32)' 方法,并且此方法無法轉換為存盤運算式。
uj5u.com熱心網友回復:
LINQ to Entities要求將整個 LINQ 查詢運算式轉換為 SQL 查詢。該Truncate函式無法由 EF 翻譯,這就是您收到該錯誤的原因。
您可以嘗試DbFunctions.Left,它回傳字串中給定數量的最左側字符。
context.Items.Select(x => new ItemModel
Zipcode = DbFunctions.Left(x.Address.Zipcode, 6)
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/515605.html
