NotSupportedException:不能将常量1表达式转换为字符串

本文关键字:表达式 转换 字符串 常量 不能 NotSupportedException | 更新日期: 2023-09-27 18:15:08

我正在手动构建一个表达式,它使用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; }
}

给定上面的类,我创建了一个等价于下面的表达式,其中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变量的值。这意味着什么,我该怎么做?

NotSupportedException:不能将常量1表达式转换为字符串

编辑:

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

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

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

public long Id {get; private set;}

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

var result = Container.Entities.Where (e => e.Id.Id.Equals(1));