在 EF 4.0 中将 String 转换为 Int [英] Convert String to Int in EF 4.0

查看:43
本文介绍了在 EF 4.0 中将 String 转换为 Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法做到这一点?我在数据库中有一个字符串字段,我想在我的 LINQ 查询中将它解析为一个 int 属性(是的,它必须在 IQueryable 级别,而不是在内存中).

Is there any way of doing this at all? I have a string field in the DB and I want to parse it into a int property in my LINQ query (yes, it must be at the IQueryable level, not in memory).

我知道 2 年前 EF 1.0 无法做到这一点(即使 LINQ to SQL 支持这种开箱即用的基本功能)......但我只是想知道是否有人想出了一种方法来做到这一点这一点?

I know 2 years ago EF 1.0 couldn't do this (even though LINQ to SQL supported this basic functionality out of the box)...but I'm just wondering if anyone has come up with a way of doing this at this point?

自定义函数映射?特殊语法?什么都....

Custom function mappings? Special syntax? Anything at all....

更新:

我尝试了一个模型定义的函数,如下所示:

I tried a model defined function as follows:

    <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.");
    }

但它似乎不起作用.我得到运行时异常:

but it doesn't seem to work. I get the runtime exception:

        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)
             at System.Data.Common.EntitySql.CqlQuery.<AnalyzeQueryExpressionSemantics>b__8(SemanticAnalyzer analyzer, Node astExpr)
             at System.Data.Common.EntitySql.CqlQuery.AnalyzeSemanticsCommon[TResult](Node astExpr, Perspective perspective, ParserOptions parserOptions, IEnumerable`1 parameters, IEnumerable`1 variables, Func`3 analysisFunction)
             at System.Data.Common.EntitySql.CqlQuery.AnalyzeQueryExpressionSemantics(Node astQueryCommand, Perspective perspective, ParserOptions parserOptions, IEnumerable`1 parameters, IEnumerable`1 variables)
             at System.Data.Common.EntitySql.CqlQuery.<>c__DisplayClass4.<CompileQueryCommandLambda>b__3(Node astCommand, ParserOptions validatedParserOptions)
             at System.Data.Common.EntitySql.CqlQuery.CompileCommon[TResult](String commandText, Perspective perspective, ParserOptions parserOptions, Func`3 compilationFunction)
             at System.Data.Common.EntitySql.CqlQuery.CompileQueryCommandLambda(String queryCommandText, Perspective perspective, ParserOptions parserOptions, IEnumerable`1 parameters, IEnumerable`1 variables)
             at System.Data.Mapping.ViewGeneration.Utils.ExternalCalls.CompileFunctionDefinition(String functionFullName, String functionDefinition, IList`1 functionParameters, EdmItemCollection edmItemCollection)
             at System.Data.Metadata.Edm.EdmItemCollection.GenerateFunctionDefinition(EdmFunction function)
             at System.Data.Common.Utils.Memoizer`2.<>c__DisplayClass2.<Evaluate>b__0()
             at System.Data.Common.Utils.Memoizer`2.Result.GetValue()
             at System.Data.Common.Utils.Memoizer`2.Evaluate(TArg arg)
             at System.Data.Metadata.Edm.EdmItemCollection.GetGeneratedFunctionDefinition(EdmFunction function)
             at System.Data.Metadata.Edm.MetadataWorkspace.GetGeneratedFunctionDefinition(EdmFunction function)
             at System.Data.Query.PlanCompiler.ITreeGenerator.Visit(DbFunctionExpression e)
        InnerException: 

更新:我让它按如下方式工作

UPDATE: I got it to work as follows

 <Function Name="ConvertToInt32" ReturnType="Edm.Int32">
      <Parameter Name="v" Type="Edm.String" />
      <DefiningExpression>
        CAST(v AS Edm.Int32)
      </DefiningExpression>
    </Function>

推荐答案

如果您使用的是 EFv4 + EDMX,您可以 创建自定义模型定义函数,它将执行CAST 为您服务.然后,您可以在 Linq-to-entities 查询中使用该函数.

If you are using EFv4 + EDMX you can create custom model defined function which will do the CAST for you. You can then use that function in Linq-to-entities queries.

这篇关于在 EF 4.0 中将 String 转换为 Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆