Accepted answer

i found out that visual studio translates it in to selects, so i realized that, the best solution for stuff like this is to make some view.. just giving answer to own q for another guys.


even though you can get some performance by removing the first, unneeded select (depending on the volume of data this could be minimal to medium improvement) like this:

keyvalues = item.resources
                .groupby(g => new { g.resourceid, g.language })                  
                .orderbydescending(o => o.changed.hasvalue ? o.changed : o.created)
                .select( s => new keyvalues
                    language = s.language,
                    keyvalue = s.value

depending on your case, you could:

  1. if your data is in a database, you can create database improvements like adding indexes, updating statistics, using hints etc.

  2. if this is local data, you can use some strategy to split new and old data between various enumerables.

there is no other way to significantly improve your linq query. you need to find another strategy to achieve that.


as you need only one element after grouping, you can return it right in groupby clause, it will simplify your code:

keyvalues = item.resources
    .groupby(g => new { g.resourceid, g.language }, 
         (x, y) => new { max = y.orderbydescending(o => o.changed ?? o.created).first() })
    .select(s => new keyvalues
        language = s.max.language,
        keyvalue = s.max.value

Related Query

More Query from same tag