我正在使用 OData Nuget 包开发 Web API 原型。我在使 LINQ to EF 查询正常工作时遇到了一些问题。
这是我的数据模型。它已被高度简化。
我正在尝试使用此 DTO 使查询正常工作:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<Order> Orders { get; set; }
}
public class Order
{
public int Id { get; set; }
public DateTime Date { get; set; }
}
查询如下所示:
[Queryable]
public IQueryable<Product> Get()
{
var productA = _context.ProductA
.Select(p => new Product
{
Id = p.id,
Name = p.name,
Orders = p.ProductAOrders.Select(o => new Order
{
Id = o.OrderId,
Date = o.Orders.Date,
})
});
var productB = _context.ProductB
.Select(p => new Product
{
Id = p.Id,
Name = p.Name,
Orders = p.ProductBOrders.Select(o => new Order
{
Id = o.OrderId,
Date = o.Orders.Date,
})
});
return productA.Union(productB);
}
当尝试联合这两个查询时,我收到此错误:
<Error><Message>An error has occurred.</Message><ExceptionMessage>The 'Distinct' operation cannot be applied to the collection ResultType of the specified argument.
Parameter name: argument</ExceptionMessage><ExceptionType>System.ArgumentException</ExceptionType><StackTrace> at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.ValidateDistinct(DbExpression argument)
at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.UnionTranslator.TranslateBinary(ExpressionConverter parent, DbExpression left, DbExpression right)
at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.BinarySequenceMethodTranslator.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.ToTraceString()
at System.Data.Entity.Internal.Linq.InternalQuery`1.ToString()
at System.Data.Entity.Infrastructure.DbQuery`1.ToString()
at System.Convert.ToString(Object value, IFormatProvider provider)
at System.Web.Http.Tracing.Tracers.HttpActionDescriptorTracer.<ExecuteAsync>b__2(TraceRecord tr, Object value)
at System.Web.Http.Tracing.ITraceWriterExtensions.<>c__DisplayClass1b`1.<>c__DisplayClass1f.<TraceBeginEndAsync>b__13(TraceRecord traceRecord)
at System.Web.Http.Tracing.SystemDiagnosticsTraceWriter.Trace(HttpRequestMessage request, String category, TraceLevel level, Action`1 traceAction)
at System.Web.Http.Tracing.ITraceWriterExtensions.<>c__DisplayClass1b`1.<TraceBeginEndAsync>b__12(TResult result)
at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass3b`2.<Then>b__3a(Task`1 t)
at System.Threading.Tasks.TaskHelpersExtensions.ThenImpl[TTask,TOuterResult](TTask task, Func`2 continuation, CancellationToken cancellationToken, Boolean runSynchronously)
</StackTrace></Error>
我可以返回 productA 或 productB - 但返回这 2 个查询的 Union 会导致上述明显错误。
对我可能做错的事情有任何想法吗?