Page 1 of 1

Linq OrderByDescending and First problem

Posted: Mon 27 Apr 2009 04:04
by pleb
Hello,

I'm not sure if this is a bug or if it's not possible.

Code: Select all

CREATE TABLE  `RealGamesFeedData`.`ProductHistoryLogs` (
  `ProductHistoryLogId` char(36) NOT NULL,
  `ProductId` char(36) NOT NULL,
  `Action` enum('Updated','Created','Deleted') NOT NULL,
  `Entry` datetime NOT NULL,
  PRIMARY KEY  (`ProductHistoryLogId`),
  KEY `IN_ProductHistoryLogs_Entry` (`Entry`),
  KEY `IN_ProductHistoryLogs_Action` (`Action`),
  KEY `FK_ProductHistoryLogs_Product` (`ProductId`),
  CONSTRAINT `FK_ProductHistoryLogs_Product` FOREIGN KEY (`ProductId`) REFERENCES `Products` (`ProductId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I'm trying to get the first result of the rows return when I order the table by entryDate desc and where product id = some product id.

After playing around I found that this works

Code: Select all

context.ProductHistoryLogSet.Where(p => p.Products.ProductId == "d27248b8-604a-bd93-0ed6-81c3033da219").OrderByDescending(log => log.Entry).First()
And this does not (which I realise now the first is a better way to do it)

Code: Select all

context.ProductHistoryLogSet.OrderByDescending(log => log.Entry).First(p => p.Products.ProductId == "d27248b8-604a-bd93-0ed6-81c3033da219")
When I load DbMonitor I can see that in the latter the order by is being left out.

Should these both not return the same result?

Thanks

Pleb.

Posted: Mon 27 Apr 2009 08:24
by AndreyR
This problem is associated with some LINQ to Entities limitations.
Please refer to the "Ordering Information Lost" section of this article:
http://msdn.microsoft.com/en-us/library/bb896317.aspx

Posted: Mon 27 Apr 2009 22:36
by pleb
Thanks AndreyR, this has cleared it up for me :D