c# - Ordering not working in Entity Framework query -


i'm battling linq query application using entity framework (6.1.3)

the query follows:

var productperiods = (from pp in ctx.productperiods                                             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(); 

the purpose of query find latest price prices collection of product period, grouped firm id , select best price of latest prices each firm.

this works in linqpad, first orderbydescending(x => x.created) doesn't work when used in context of entity framework.

does knows why? , perhaps have solution it? :-)

thanks in advance!

update

thanks replies. i've tried following:

select new {     productperiod = p,       price = p.prices.groupby(x => x.firmid).select(pr => pr.orderbydescending(x => x.created).thenbydescending(x => x.productprice).firstordefault()) } 

but seems thenbydescending(x => x.productprice) gets ignored well. prices not sorted correctly in output. they're output 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 

update 2 (solution now)

i came solution initial query returns latest prices each firm. there's 3 firms, performance should alright.

later in code, i'm using latest , best price, .orderbydescending(x => x.productprice).firstordefault() , check if it's not null.

i.e:

var productperiods = (from pp in ctx.productperiods                       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(); 

later in code:

var bestpriceoftoday = period.prices.orderbydescending(x => x.productprice).firstordefault() 

the problem commands using. orderby , orderbydescending not add additional order statements resulting query instead create order statement , eliminate orderby statements existed before.

in order use multiple orderby's need following:

  • orderby or orderbydescending
  • thenby or thenbydescending

the thenby statements can used 1 or more times add additional order statements resulting query.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -