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

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

问题描述

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



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



自定义函数映射?特殊语法?任何东西...



更新:



我尝试了一个模型定义的函数如下: p>

 < Function Name =ConvertToInt32ReturnType =Edm.Int32> 
< Parameter Name =vType =Edm.String/>
< DefiningExpression>
CAST(v AS INT)
< / DefiningExpression>
< / Function>

[EdmFunction(Model.Repository,ConvertToInt32)]
public static int ConvertToInt32(string value)
{
throw new InvalidOperationException(Only valid当用作LINQ查询的一部分。);
}

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

  ErrorDescription =无法找到类型'INT'。确保加载所需的模式,并正确导入命名空间。 
StackTrace:
在System.Data.Common.EntitySql.SemanticAnalyzer.ConvertTypeName(Node typeName,SemanticResolver sr)
在System.Data.Common.EntitySql.SemanticAnalyzer.ConvertTypeExprArgs(BuiltInExpr astBuiltInExpr,SemanticResolver sr)
在System.Data.Common.EntitySql.SemanticAnalyzer。< CreateBuiltInExprConverter> b__73(BuiltInExpr bltInExpr,SemanticResolver sr)
在System.Data.Common.EntitySql.SemanticAnalyzer.ConvertBuiltIn(Node astExpr,SemanticResolver sr)
在System.Data.Common.EntitySql.SemanticAnalyzer.Convert(Node astExpr,SemanticResolver sr)
在System.Data.Common.EntitySql.SemanticAnalyzer.ConvertValueExpression(Node astExpr,SemanticResolver sr)
在System.Data.Common.EntitySql.SemanticAnalyzer.ConvertQueryStatementToDbExpression(Statement astStatement,SemanticResolver sr)
在System.Data.Common.EntitySql.SemanticAnalyzer.An alyzeQueryCommand(Node astExpr)
在System.Data.Common.EntitySql.CqlQuery。< AnalyzeQueryExpressionSemantics> b__8(SemanticAnalyzer analyzer,Node astExpr)
在System.Data.Common.EntitySql.CqlQuery.AnalyzeSemanticsCommon [TResult ](Node astExpr,Perspective透视图,ParserOptions parserOptions,IEnumerable`1参数,IEnumerable`1变量,Func`3分析功能)
在System.Data.Common.EntitySql.CqlQuery.AnalyzeQueryExpressionSemantics(Node astQueryCommand,Perspective perspective,ParserOptions parserOptions,IEnumerable`1参数,IEnumerable`1变量)
在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)
在System.Data.Common.EntitySql.CqlQuery.CompileQueryCommandLambda(String queryCommandText,Perspective透视图,ParserOptions parserOptions,IEnumerable`1参数,IEnumerable`1变量)
在System.Data.Mapping.ViewGeneration.Utils.ExternalCalls.CompileFunctionDefinition (String functionFullName,String functionDefinition,IList`1 functionParameters,EdmItemCollection edmItemCollection)
在System.Data.Metadata.Edm.EdmItemCollection.GenerateFunctionDefinition(EdmFunction函数)
在System.Data.Common.Utils.Memoizer` 2. System.Data.Common.Utils.Memoizer`2.Result.GetValue()
在System.Data.Common.Utils中。  c__DisplayClass2。评估> b__0()
。 Memoizer`2.Evaluate(TArg arg)
在System.Data.Metadata.Edm.EdmItemCollection.GetGeneratedFunctionDefinition(EdmFunction函数)
在System.Data.Metadata.Edm.MetadataWorkspace.GetGeneratedFunctionDefini (EdmFunction函数)
在System.Data.Query.PlanCompiler.ITreeGenerator.Visit(DbFunctionExpression e)
InnerException:

更新:我的工作如下

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


解决方案

如果您使用的是EFV4 + EDMX, href =https://stackoverflow.com/questions/5612827/entity-framework-where-do-i-extend-the-csdl-msl/5613642#5613642>创建自定义模型定义函数,这将做您的 CAST 。然后,您可以在Linq-to-entities查询中使用该功能。


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).

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....

UPDATE:

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>

解决方案

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中将字符串转换为Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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