Wcf Services查询中的内部查询抛出"NotSupportedException" [英] Inner query inside Wcf Services query throws 'NotSupportedException'

查看:88
本文介绍了Wcf Services查询中的内部查询抛出"NotSupportedException"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有许多关系的类,并且在正确过滤使用Wcf数据服务(通过

I have a class with a many-many relationship and I'm having problems in properly filtering the results from a query made using Wcf Data Services (through the DataServiceQuery class). This service exposes an Entity Framework 5 model.

这个简单的查询例如:

from device in devices
select new 
{
    DeviceName = device.Name,
    TestUsers = device
        .Allocations
        .Where(allocation => allocation.User == "testUser")
}

这在运行时给了我NotSupportedException:

[NotSupportedException: Constructing or initializing instances of the type <>f__AnonymousType0`4[System.String,System.String,System.String,System.Collections.Generic.IEnumerable`1[Mobiltec.M3S.Model.AllocInfo]] with the expression device.Allocations.Where(_allocation => (_allocation.User == "testUser")) is not supported.]
   System.Data.Services.Client.NonEntityProjectionAnalyzer.VisitMethodCall(MethodCallExpression m) +650
   System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp) +456
   System.Data.Services.Client.ALinqExpressionVisitor.VisitExpressionList(ReadOnlyCollection`1 original) +107
   System.Data.Services.Client.ALinqExpressionVisitor.VisitNew(NewExpression nex) +52
   System.Data.Services.Client.NonEntityProjectionAnalyzer.VisitNew(NewExpression nex) +226
   System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp) +552
   System.Data.Services.Client.NonEntityProjectionAnalyzer.Analyze(Expression e, PathBox pb, DataServiceContext context) +285
   System.Data.Services.Client.ProjectionAnalyzer.Analyze(LambdaExpression e, PathBox pb, DataServiceContext context) +226
   System.Data.Services.Client.ProjectionAnalyzer.AnalyzeResourceExpression(LambdaExpression lambda, ResourceExpression resource, DataServiceContext context) +58
   System.Data.Services.Client.ProjectionAnalyzer.Analyze(LambdaExpression le, ResourceExpression re, Boolean matchMembers, DataServiceContext context) +335
   System.Data.Services.Client.ResourceBinder.AnalyzeProjection(MethodCallExpression mce, SequenceMethod sequenceMethod, Expression& e) +1000
   System.Data.Services.Client.ResourceBinder.VisitMethodCall(MethodCallExpression mce) +149
   System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp) +456
   System.Data.Services.Client.ALinqExpressionVisitor.VisitExpressionList(ReadOnlyCollection`1 original) +107
   System.Data.Services.Client.ALinqExpressionVisitor.VisitMethodCall(MethodCallExpression m) +87
   System.Data.Services.Client.ResourceBinder.VisitMethodCall(MethodCallExpression mce) +177
   System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp) +456
   System.Data.Services.Client.ResourceBinder.Bind(Expression e, DataServiceContext context) +57
   System.Data.Services.Client.DataServiceQueryProvider.Translate(Expression e) +252
   System.Data.Services.Client.DataServiceQuery`1.Translate() +37
   System.Data.Services.Client.DataServiceRequest.GetQuerySetCount(DataServiceContext context) +77
   System.Data.Services.Client.DataServiceQueryProvider.ReturnSingleton(Expression expression) +332

如果我只在其中投影数据,而不是过滤,就像这样:

If I only project data in there, instead of filtering, like this:

from device in devices
select new 
{
    DeviceName = device.Name,
    AllocatedUsers = device
        .Allocations
        .Select(allocation => allocation.User)
}

它按预期工作.

推荐答案

OData的$ select子句(Linq select映射到URL中的子句)不支持转换属性值的投影,至少在OData v3中.投影更多地用于减少需要使用的属性数量,而不是操纵属性值.

OData's $select clause (which is what the Linq select maps to in the URL) does not support projections that transform property values, at least in OData v3. Projections are used more to reduce the number of properties that you need to work with, rather than to manipulate the property values.

这很好并且受支持:

from device in devices
select new 
{
    DeviceName = device.Name,
    TestUsers = device.Allocations
}

,但是只要将.Where子句放在Allocations上,就无法将其转换为OData URL语法.

but as soon as you put the .Where clause on Allocations, there is no way to transform that into OData URL syntax.

这篇关于Wcf Services查询中的内部查询抛出"NotSupportedException"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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