score:1
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.
Source: stackoverflow.com
Related Articles
- Improve LINQ performance
- Will indexing a SQL table improve the performance of a LINQ statement?
- LINQ Source Code Available
- How to improve LINQ to EF performance
- creating Linq to sqlite dbml from DbLinq source code
- Improve the performance of an AutoComplete LINQ Query
- Improve performance on LINQ Query
- source code for LINQ 101 samples
- Improve Linq to Datatable Performance
- Linq join on objects with navigation property - how to improve performance
- How to improve performance of my linq query?
- Improve LINQ query performance
- how to improve LInQ performance repository pattern
- How to improve the performance of a LINQ query that compares two lists?
- c# Linq or code to extract groups from a single list of source data
- Improve EF Linq query performance
- Improve performance in linq query - trying to change the query from subquery to join
- Improve Linq query performance that use ToList()
- How to improve linq performance with large collections (Code first POCO class)
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- LINQ performance FAQ
- How are people unit testing code that uses Linq to SQL
- Performance of LINQ Any vs FirstOrDefault != null
- foreach + break vs linq FirstOrDefault performance difference
- For vs. Linq - Performance vs. Future
- What is the Efficiency and Performance of LINQ and Lambda Expression in .Net?
- LINQ performance Count vs Where and Count
- Aggregate vs Sum Performance in LINQ
- How to group and rank with reference to another column
- linq changing order of columns
- Prevent 'NOW()' in LINQ to EF query
- Using linq with Sharepoint and disposing of objects
- Linq query, how to orderby
- Linq, double left join and double count
- How to cast LINQ generated anonymous data type list from DataView ASP .NET MVC?
- Ignore dots in Convert.ToDecimal and return 0
- Lambda expression question
- How can I perform this type of JOIN in Entity framework with LINQ
- Is there equivalent statement in EF for the LINQ expression?
- Create LINQ query at runtime to GroupBy in EntityFramework (with inheritance)
- How to cast derived class to base with new properties?
- Join the datatable using Linq
- Get DataTime DataColumn of DataTable to List
- How to refactor this linq query?
- How to do an Inner Join in LINQ
- Linq Select: Set specific value for first occurrence only
- Linq query to filter out list of list without using Remove
- Concatenate string in Linq query