Linq OrderByDescending and First problem

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
pleb
Posts: 37
Joined: Mon 23 Jun 2008 06:21
Location: Melbourne, Australia

Linq OrderByDescending and First problem

Post by pleb » Mon 27 Apr 2009 04:04

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.

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Mon 27 Apr 2009 08:24

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

pleb
Posts: 37
Joined: Mon 23 Jun 2008 06:21
Location: Melbourne, Australia

Post by pleb » Mon 27 Apr 2009 22:36

Thanks AndreyR, this has cleared it up for me :D

Post Reply