score:0

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.

score:0

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
                .where(conditionexpression)
                .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
                }).tolist();

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.

score:2

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

keyvalues = item.resources
    .where(conditionexpression)
    .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
    })
    .tolist();

Related Query

More Query from same tag