WCF数据服务如何查询一个集合的导航属性

时间:2012-01-19 20:51:35

标签: wcf c#-4.0 entity-framework-4.1 wcf-data-services

我有一个我正在处理的项目,它有一个Order实体,它有一个OrderITems的导航属性:

public class Order
{
   public int OrderId { get; set; }
   public int CustomerId { get; set; }

   public virtual ICollection<OrderItem> OrderItems { get; set; }
}

public class OrderItem
{
   public int OrderId { get; set; }
   public int ProductId { get; set; }
   public int Quantity { get; set; }

   public virtual Order Order { get; set; }
}

我希望能够使用针对OrderItems集合的过滤器来查询订单

例如: http:\ mysvc.com \ mysvc \ Orders?$ filter = OrderItems \ ProductId eq 1234

然而,我似乎无法让这个工作。有任何想法吗?我很乐意提供更多细节。

Project正在使用EntityFramework代码,首先是4.1数据提供程序。

更新了异常和callstack:

Not Implemented Unable to create a constant value of type 'System.Data.Services.Internal.ProjectedWrapper1'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.System.NotSupportedException   at System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq)

   at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)

   at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

   at System.Data.Objects.ELinq.ExpressionConverter.ConditionalTranslator.TypedTranslate(ExpressionConverter parent, ConditionalExpression linq)

   at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)

   at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

   at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)

   at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding)

   at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)

   at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)

   at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)

   at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)

   at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)

   at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

   at System.Data.Objects.ELinq.ExpressionConverter.Convert()

   at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)

   at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)

   at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()

   at System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()

   at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.IEnumerable.GetEnumerator()

   at System.Data.Services.Internal.ProjectedWrapper.EnumerableWrapper.System.Collections.IEnumerable.GetEnumerator()

   at System.Data.Services.WebUtil.GetRequestEnumerator(IEnumerable enumerable)
    

1 个答案:

答案 0 :(得分:1)

使用当前发布的位,你必须转向查询:

/ OrderItems?$ expand = Order&amp; $ filter = ProductId eq 1234

这种方法的缺点是,如果单个订单有两个带有过滤产品ID的OrderItem,您将获得两次相同的订单。您还将获得所有已过滤的OrderItem。您可以使用$ select来缩小下载的数据量。

在最新的CTP(http://blogs.msdn.com/b/astoriateam/archive/2011/10/13/announcing-wcf-data-services-oct-2011-ctp-for-net-4-and-silverlight-4.aspx)中,支持any / all,这就是你在这里使用的。有关详细信息,请参阅此博客文章http://www.odata.org/blog/even-more-any-and-all