我正在手动构建一个Expression,它使用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
变量的值。这意味着什么,我该怎么办呢?
答案 0 :(得分:2)
编辑:
真正的问题可能是它们查询不处理EntityIdentity类,因为它可能只能处理原语。也许它试图将类转换为字符串。
我的建议是只在查询中使用原语。
在EntityIdentity中,将Id属性更改为public:
public long Id {get; private set;}
然后查询表达式变为:
var result = Container.Entities.Where (e => e.Id.Id.Equals(1));