score:1

Accepted answer

maybe this can help. where db is the database context:

(
            from pv1 in db.propertyvalues
            from pv2 in db.propertyvalues.where(a=>a.property_id==pv1.property_id && pv1.revision<pv2.revision).defaultifempty()
            join p in db.properties
                on pv1.property_id equals p.id
            where pv2.id==null
            orderby p.id
            select new
            {
                p.id,
                pv1.stringvalue,
                pv1.revision
            }
    );

score:0

if you want to use multiple conditions (less than expression) in join you can do this like

from pv1 in db.propertyvalues
 join pv2 in db.propertyvalues on new{pv1.property_id, condition = pv1.revision < pv2.revision} equals new {pv2.property_id , condition = true} into temp
from t in temp.defaultifempty()
            join p in db.properties
                on pv1.property_id equals p.id
            where t.id==null
            orderby p.id
            select new
            {
                p.id,
                pv1.stringvalue,
                pv1.revision
            }

score:1

next to optimizing a query in linq to entities, you also have to be aware of the work it takes for the entity framework to translate your query to sql and then map the results back to your objects.

comparing a linq to entities query directly to a sql query will always result in lower performance because the entity framework does a lot more work for you.

so it's also important to look at optimizing the steps the entity framework takes.

things that could help:

  • precompile your query
  • pre-generate views
  • decide for yourself when to open the database connection
  • disable tracking (if appropriate)

here you can find some documentation with performance strategies.


Related Query

More Query from same tag