The method Last is not recognized by LINQ To Entities. Really?

c# entity-framework orm

Question

In this case:

public static IEnumerable<IServerOnlineCharacter> GetUpdated()
{
    var context = DataContext.GetDataContext();
    return context.ServerOnlineCharacters
        .OrderBy(p => p.ServerStatus.ServerDateTime)
        .GroupBy(p => p.RawName)
        .Select(p => p.Last());
}

For it to function, I had to change it to this.

public static IEnumerable<IServerOnlineCharacter> GetUpdated()
{
    var context = DataContext.GetDataContext();
    return context.ServerOnlineCharacters
        .OrderByDescending(p => p.ServerStatus.ServerDateTime)
        .GroupBy(p => p.RawName)
        .Select(p => p.FirstOrDefault());
}

I was unable to utilizep.First() to confirm the first inquiry.

Why does such a strong ORM system have such fundamental limitations?

1
143
12/8/2016 8:53:54 AM

Accepted Answer

Due to the fact that SQL has a restriction where it must finally transform the query to SQL,SELECT TOP but not a (in T-SQL)SELECT BOTTOM (This is untrue.

However, there is a simple workaround. Simply order downward and then do aFirst() and that's what you did.

EDIT: It's possible that other providers may use different versions ofSELECT TOP 1 , on Oracle it would likely be more along the lines ofWHERE ROWNUM = 1

EDIT:

Zzz-47-Zzz is a less effective option that you may use..ToList() previously based on your data.Last() , which will then instantly run your and the LINQ To Entities expression you had developed up to that moment. Last() will work, because at that point the.Last() is really carried out inside the framework of a "LINQ to Objects" Expression. (And as you said, it might recover hundreds of records and utilize a ton of CPU power to create inert objects.)

Even while I once again do not advise doing this second, it does assist to highlight the distinction between where and when the LINQ expression is run.

218
9/3/2011 2:30:09 PM

Popular Answer

in place ofLast() , Do this:

model.OrderByDescending(o => o.Id).FirstOrDefault();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow