订单在实体框架查询中无法工作 [英] Ordering not working in Entity Framework query
问题描述
我目前正在使用实体框架(6.1.3)对我的应用程序进行一个linq查询。
I'm currently battling a linq query for my application using Entity Framework (6.1.3)
查询如下:
var productPeriods = (from pp in ctx.ProductPeriods
where pp.IsActive && pp.Product.IsBuyBackForProduct == null && !pp.Product.ProductAddOns.Any() && pp.PowerRegionID == powerRegionId
select new
{
ProductPeriod = pp,
Price = pp.Prices
.OrderByDescending(x => x.Created)
.GroupBy(x => x.FirmID)
.Select(pr => pr.FirstOrDefault())
.OrderByDescending(x => x.ProductPrice)
.FirstOrDefault()
}).ToList();
查询的目的是从产品期间的价格收集中查找最新价格,分组通过企业ID,然后从每个公司选择最新价格的最佳价格。
The purpose of the query is to find the latest price from the prices collection of a product period, grouped by the firm ID and then select the best price of the latest prices from each firm.
这在Linqpad中完美无缺,但第一个 OrderByDescending x => x.Created)
在实体框架的上下文中使用时不起作用。
This works perfectly in Linqpad, but the first OrderByDescending(x => x.Created)
doesn't work when used in context of Entity Framework.
有没有人知道为什么?也许有解决办法吗? : - )
Does anyone knows why? And perhaps have a solution for it? :-)
提前感谢!
更新
感谢所有回复。我尝试了以下内容:
Thanks for all replies. I've tried the following:
select new {
ProductPeriod = p,
Price = p.Prices.GroupBy(x => x.FirmID).Select(pr => pr.OrderByDescending(x => x.Created).ThenByDescending(x => x.ProductPrice).FirstOrDefault())
}
但它似乎像 ThenByDescending(x => x.ProductPrice )
也被忽略。价格在输出中没有正确排序。他们输出如下:
But it seems like ThenByDescending(x => x.ProductPrice)
gets ignored as well. The prices are not sorted correctly in the output. They're output like this:
Price: 0,22940, Created: 06-03-2015 10:15:09,
Price: 0,23150, Created: 06-03-2015 10:05:48
Price: 0,20040, Created: 06-03-2015 09:24:24
更新2(现在的解决方案)
我来到解决方案,初始查询只是从每个公司返回最新的价格。目前有三家公司,所以表现应该是好的。
I came to the solution that the initial query just returns the latest prices from each firm. There's currently three firms, so the performance should be alright.
在我的代码中,在实际使用最新最优惠价格的地方,我只需要一个 .OrderByDescending(x => x.ProductPrice).FirstOrDefault()
并检查它是否不为空。
Later in my code, where I'm actually using the latest and best price, I simply do an .OrderByDescending(x => x.ProductPrice).FirstOrDefault()
and check if it's not null.
即:
var productPeriods = (from pp in ctx.ProductPeriods
where pp.IsActive && pp.Product.IsBuyBackForProduct == null && !pp.Product.ProductAddOns.Any() && pp.PowerRegionID == powerRegionId
select new
{
ProductPeriod = pp,
Prices = pp.Prices.GroupBy(x => x.FirmID).Select(pr => pr.OrderByDescending(x => x.Created).FirstOrDefault())
}).ToList();
稍后在我的代码中:
var bestPriceOfToday = period.Prices.OrderByDescending(x => x.ProductPrice).FirstOrDefault()
推荐答案
根据您的更新,omnit 选择
并键入:
According to yours update, omnit select
and type:
select new {
ProductPeriod = p,
Price = p.Prices.GroupBy(x => x.FirmID)
.OrderByDescending(x =>x.Created).ThenByDescending(x=>x.ProductPrice).FirstOrDefault()
}
选择
是无用的,可能是问题的原因
That select
was useless and could be the cause of problem
这篇关于订单在实体框架查询中无法工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!