有没有办法做到这一点?我在DB中有一个字符串字段,我想在我的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.");
}
Run Code Online (Sandbox Code Playgroud)
但它似乎没有用.我得到运行时异常:
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) …Run Code Online (Sandbox Code Playgroud)