0

我使用 WCF 数据服务并有一个运行良好的 LINQ to Entities 查询:

var result = 
   (from data1 in context.Data
    join info in context.Information on data1.inf_ID equals info.ID
    select info );
return result.AsQueryable();

如果我使用以下代码:

string[] ids ={"1","2","3"};

var result = 
   (from data1 in context.Data
    join info in context.Information on data1.inf_ID equals info.ID
    where ids.Contains(info.ID.ToString())
    select info);
return result.AsQueryable();

我得到以下异常和堆栈跟踪:

Exception has been thrown by the target of an invocation.
Stacktrace: at: System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at: System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at: System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at: System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at: System.Data.Services.Providers.BaseServiceProvider.InvokeServiceOperation(ServiceOperation serviceOperation, Object[] parameters)
at: System.Data.Services.Providers.EntityFrameworkDataServiceProvider.InvokeServiceOperation(ServiceOperation serviceOperation, Object[] parameters)
at: System.Data.Services.Providers.DataServiceProviderWrapper.InvokeServiceOperation(OperationWrapper serviceOperation, Object[] parameters)
at: System.Data.Services.RequestUriProcessor.ComposeExpressionForServiceOperation(SegmentInfo segment, IDataService service, Boolean checkRights, SegmentInfo lastSegment)
at: System.Data.Services.RequestUriProcessor.ComposeExpressionForSegments(ICollection`1 parsedSegments, IDataService service)
at: System.Data.Services.RequestUriProcessor.ProcessRequestUri(Uri absoluteRequestUri, IDataService service, Boolean internalQuery)
at: System.Data.Services.DataService`1.ProcessIncomingRequestUri()
at: System.Data.Services.DataService`1.HandleRequest()

INNER EXCEPTION: null
Stacktrace: at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.ContainsTranslator.TranslateContains(ExpressionConverter parent, Expression sourceExpression, Expression valueExpression)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.ContainsTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.BinaryTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.BinaryTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)
at: System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
at: System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at: System.Data.Objects.ELinq.ExpressionConverter.Convert()
at: System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
at: System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at: System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
at: System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()
at: System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
at: System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at: System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at: InfoService.GetIDS(String ids) at: InfoService.svc.cs, line: 507

at: System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at: System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at: System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at: System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at: System.Data.Services.Providers.BaseServiceProvider.InvokeServiceOperation(ServiceOperation serviceOperation, Object[] parameters)
at: System.Data.Services.Providers.EntityFrameworkDataServiceProvider.InvokeServiceOperation(ServiceOperation serviceOperation, Object[] parameters)
at: System.Data.Services.Providers.DataServiceProviderWrapper.InvokeServiceOperation(OperationWrapper serviceOperation, Object[] parameters)
at: System.Data.Services.RequestUriProcessor.ComposeExpressionForServiceOperation(SegmentInfo segment, IDataService service, Boolean checkRights, SegmentInfo lastSegment)
at: System.Data.Services.RequestUriProcessor.ComposeExpressionForSegments(ICollection`1 parsedSegments, IDataService service)
at: System.Data.Services.RequestUriProcessor.ProcessRequestUri(Uri absoluteRequestUri, IDataService service, Boolean internalQuery)
at: System.Data.Services.DataService`1.ProcessIncomingRequestUri()
at: System.Data.Services.DataService`1.HandleRequest()

我了解到 EF4 支持包含。如何为此创建解决方法?

4

1 回答 1

1

不要使用字符串数组和 info.ID.ToString() 方法,保留你的 int/double/decimal.. 它会解决的。

您必须使用以下方法将 int 转换为字符串并创建 where 子句: SqlFunctions.StringConvert http://msdn.microsoft.com/en-us/library/dd466276.aspx

编辑:

//if info.ID is a string this is sufficient (.ToString() was removed)
string[] ids ={"1","2","3"};

var result = 
   (from data1 in context.Data
    join info in context.Information on data1.inf_ID equals info.ID
    where ids.Contains(info.ID)
    select info);
return result.AsQueryable();
于 2013-09-24T09:51:03.850 回答