我有一个具有多对多关系的类,并且在正确过滤使用 Wcf 数据服务(通过DataServiceQuery
该类)进行的查询的结果时遇到问题。该服务公开了一个 Entity Framework 5 模型。
例如这个简单的查询:
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
如果我只在那里投影数据,而不是过滤,像这样:
from device in devices
select new
{
DeviceName = device.Name,
AllocatedUsers = device
.Allocations
.Select(allocation => allocation.User)
}
它按预期工作。