Top per group: Take(1) works but FirstOrDefault() doesn't?

后端 未结 4 2037
离开以前
离开以前 2021-02-20 15:32

I\'m using EF 4.3.1... just upgraded to 4.4 (problem remains) with database-first POCO entities generated by the EF 4.x DbContext Generator. I have the following

4条回答
  •  孤街浪徒
    2021-02-20 16:39

    As in the prev answer I tried to reason about the problem - I resigned, and I'm writing another one:) After looking at it again, I think it is a bug. I think you should just use Take and post the case to Microsoft's Connect and check what do they say about it.

    Here's what I have found: http://connect.microsoft.com/VisualStudio/feedback/details/658392/linq-to-entities-orderby-is-lost-when-followed-by-firstordefault

    The response from 'Microsoft 2011-09-22 at 16:07' describes in details some optimizing mechanisms inside EF. In a few places they say about reordering skip/take/orderby and that sometimes the logic does not recognize some constructs.. I think you have just stumbled upon another corner case that is not-yet-branched properly in the 'orderby lifting'. All in all, in the resulting SQL you have select-top-1 inside an order-by, and the damage looks exactly like lifting the 'top 1' one level too high!

提交回复
热议问题