gpt4 book ai didi

linq - Web API、OData、EF5、联合 : The 'Distinct' operation cannot be applied to the collection ResultType of the specified argument

转载 作者:行者123 更新时间:2023-12-04 18:45:19 28 4
gpt4 key购买 nike

我正在使用 OData Nuget 包开发 Web API 原型(prototype)。
我在使 LINQ to EF 查询正常工作时遇到了一些问题。

Here are my data model.它已被高度简化。

我正在尝试使用此 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.&lt;ExecuteAsync&gt;b__2(TraceRecord tr, Object value)
at System.Web.Http.Tracing.ITraceWriterExtensions.&lt;&gt;c__DisplayClass1b`1.&lt;&gt;c__DisplayClass1f.&lt;TraceBeginEndAsync&gt;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.&lt;&gt;c__DisplayClass1b`1.&lt;TraceBeginEndAsync&gt;b__12(TResult result)
at System.Threading.Tasks.TaskHelpersExtensions.&lt;&gt;c__DisplayClass3b`2.&lt;Then&gt;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 会导致上述明显错误。

对我可能做错的事情有任何想法吗?

最佳答案

看起来像一个EF错误。我假设您正在尝试使 MEST(相同类型的多个实体集)工作。您可以尝试,而不是您的查询,

public IQueryable<Product> Get()
{
var productA = _context.ProductA
.Select(p => new Product
{
Id = p.id,
Name = p.name,
});

var productB = _context.ProductB
.Select(p => new Product
{
Id = p.Id,
Name = p.Name,
});

return
productA
.Union(productB)
.Select(p => new Product
{
Id = p.Id,
Name = p.Name,
Orders = _context.Orders
.Where(o => o.ProductA.Id == p.Id || o.ProductB.Id == p.Id)
.Select(o => new Order
{
Id = o.OrderId,
Date = o.Orders.Date,
})
});
}

这个想法是将导航属性从联合中取出,然后再将它们添加回来。这只有在 Orders 有指向 ProductA 或 ProductB 的反向指针时才有效。

关于linq - Web API、OData、EF5、联合 : The 'Distinct' operation cannot be applied to the collection ResultType of the specified argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15509901/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com