1

我正在手动构建一个表达式,它使用 IEnumerable.Where() 从 WCF 数据服务客户端代理过滤 DataServiceQuery。

public class EntityIdentity
{
    public EntityIdentity(long id);
    {
        this.Id = id;
    }

    private long Id {get; private set;}

    public override bool Equals(object obj)
    {
        return obj is EntityIdentity && this.Id == (obj as EntityIdentity).Id;
    }

    public override string ToString()
    {
        return this.Id.ToString();
    }
}

public class Entity
{
    public Entity()
    {
        this.Id = new EntityIdentity(0);
        this.Name = null; 
    }

    public EntityId Id { get; set; }

    public string Name { get; set; }
}

鉴于上述类,我正在创建一个等效于以下 where 的表达式EntityIdentity targetId = new EntityIdentity(1)

container.Entities.Where(e => e.Id.Equals(targetId));

当我尝试解析此查询时,LINQ-to-URI 转换器抛出以下异常:

{System.NotSupportedException: Could not convert constant 1 expression to string.
   at System.Data.Services.Client.ExpressionWriter.VisitConstant(ConstantExpression c)
   at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.ExpressionWriter.Visit(Expression exp)
   at System.Data.Services.Client.ExpressionWriter.VisitOperand(Expression e, Nullable`1 parentType, Nullable`1 childDirection)
   at System.Data.Services.Client.ExpressionWriter.VisitBinary(BinaryExpression b)
   at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.ExpressionWriter.Visit(Expression exp)
   at System.Data.Services.Client.ExpressionWriter.VisitOperand(Expression e, Nullable`1 parentType, Nullable`1 childDirection)
   at System.Data.Services.Client.ExpressionWriter.VisitBinary(BinaryExpression b)
   at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.ExpressionWriter.Visit(Expression exp)
   at System.Data.Services.Client.ExpressionWriter.Translate(Expression e)
   at System.Data.Services.Client.ExpressionWriter.ExpressionToString(DataServiceContext context, Expression e, Boolean inPath, Version& uriVersion)
   at System.Data.Services.Client.UriWriter.ExpressionToString(Expression expression, Boolean inPath)
   at System.Data.Services.Client.UriWriter.VisitQueryOptionExpression(FilterQueryOptionExpression fqoe)
   at System.Data.Services.Client.UriWriter.VisitQueryOptions(ResourceExpression re)
   at System.Data.Services.Client.UriWriter.VisitResourceSetExpression(ResourceSetExpression rse)
   at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.UriWriter.Translate(DataServiceContext context, Boolean addTrailingParens, Expression e, Uri& uri, Version& version)
   at System.Data.Services.Client.DataServiceQueryProvider.Translate(Expression e)
   at System.Data.Services.Client.DataServiceQuery`1.Translate()
   at System.Data.Services.Client.DataServiceQuery`1.QueryComponents(ClientEdmModel model)
   at System.Data.Services.Client.DataServiceRequest.CreateExecuteResult(Object source, DataServiceContext context, AsyncCallback callback, Object state, String method)
   at System.Data.Services.Client.DataServiceRequest.BeginExecute(Object source, DataServiceContext context, AsyncCallback callback, Object state, String method)
   at System.Data.Services.Client.DataServiceQuery`1.BeginExecute(AsyncCallback callback, Object state)
   at System.Data.Services.Client.DataServiceQuery`1.BeginExecuteInternal(AsyncCallback callback, Object state)
   at System.Data.Services.Client.DataServiceQuery.BeginExecute(AsyncCallback callback, Object state)
   at ProAlign.ServerDataModule.Services.ModelSetQueryContext.<>c__DisplayClassa.<Execute>b__3()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()}

前面提到的“常数1表达式”是指targetId变量的值。这是什么意思,我该怎么办?

4

1 回答 1

2

编辑:

真正的问题可能是他们查询没有处理 EntityIdentity 类,因为它可能只能处理原语。也许它正在尝试将类转换为字符串。

我的建议是只使用查询中的原语。

在 EntityIdentity 中将 Id 属性更改为 public:

public long Id {get; private set;}

然后查询表达式变成这样:

var result = Container.Entities.Where (e => e.Id.Id.Equals(1));    
于 2013-05-27T18:47:10.743 回答