亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在EF 4.0中將字符串轉換為Int

在EF 4.0中將字符串轉換為Int

郎朗坤 2019-11-04 09:51:58
有沒有辦法做到這一點?我在數據庫中有一個字符串字段,我想將其解析為我的LINQ查詢中的int屬性(是的,它必須處于IQueryable級別,而不是位于內存中)。我知道2年前EF 1.0不能做到這一點(即使LINQ to SQL開箱即用也支持此基本功能)...但是我只是想知道現在是否有人想出辦法?自定義功能映射?特殊語法?什么都沒有...更新:我嘗試了模型定義的函數,如下所示:    <Function Name="ConvertToInt32" ReturnType="Edm.Int32">      <Parameter Name="v" Type="Edm.String" />      <DefiningExpression>        CAST(v AS INT)      </DefiningExpression>    </Function>    [EdmFunction("Model.Repository", "ConvertToInt32")]    public static int ConvertToInt32(string value)    {        throw new InvalidOperationException("Only valid when used as part of a LINQ query.");    }但它似乎不起作用。我得到運行時異常: ErrorDescription=Type 'INT' could not be found. Make sure that the required schemas are loaded and that the namespaces are imported correctly.        StackTrace:             at System.Data.Common.EntitySql.SemanticAnalyzer.ConvertTypeName(Node typeName, SemanticResolver sr)             at System.Data.Common.EntitySql.SemanticAnalyzer.ConvertTypeExprArgs(BuiltInExpr astBuiltInExpr, SemanticResolver sr)             at System.Data.Common.EntitySql.SemanticAnalyzer.<CreateBuiltInExprConverter>b__73(BuiltInExpr bltInExpr, SemanticResolver sr)             at System.Data.Common.EntitySql.SemanticAnalyzer.ConvertBuiltIn(Node astExpr, SemanticResolver sr)             at System.Data.Common.EntitySql.SemanticAnalyzer.Convert(Node astExpr, SemanticResolver sr)             at System.Data.Common.EntitySql.SemanticAnalyzer.ConvertValueExpression(Node astExpr, SemanticResolver sr)             at System.Data.Common.EntitySql.SemanticAnalyzer.ConvertQueryStatementToDbExpression(Statement astStatement, SemanticResolver sr)             at System.Data.Common.EntitySql.SemanticAnalyzer.AnalyzeQueryCommand(Node astExpr)
查看完整描述

2 回答

?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

關于在運行時將其添加到元數據工作空間的問題非常好,但是我不知道答案。無論如何,您將無法將其映射到,Convert.ToInt32因為您無法EdmFunction向該方法添加屬性。

查看完整回答
反對 回復 2019-11-04
  • 2 回答
  • 0 關注
  • 670 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號