Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6966103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:08:52+00:00 2026-05-27T16:08:52+00:00

I have two classes: Property and PropertyValue. A property has several values where each

  • 0

I have two classes: Property and PropertyValue. A property has several values where each value is a new revision.

When retrieving a set of properties I want to include the latest revision of the value for each property.

in T-SQL this can very efficiently be done like this:

SELECT 
p.Id,
pv1.StringValue,
pv1.Revision
FROM dbo.PropertyValues pv1
LEFT JOIN dbo.PropertyValues pv2 ON pv1.Property_Id = pv2.Property_Id AND pv1.Revision < pv2.Revision
JOIN dbo.Properties p ON p.Id = pv1.Property_Id
WHERE pv2.Id IS NULL
ORDER BY p.Id

The “magic” in this query is to join on the lesser than condition and look for rows without a result forced by the LEFT JOIN.

How can I accomplish something similar using LINQ to EF?

The best thing I could come up with was:

from pv in context.PropertyValues
group pv by pv.Property into g
select g.OrderByDescending(p => p.Revision).FirstOrDefault()

It does produce the correct result but is about 10 times slower than the other.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:08:54+00:00Added an answer on May 27, 2026 at 4:08 pm

    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
                }
        );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, WebServiceRequest and OrderRequest. Each class has properties. OrderRequest inherits from
I have two classes, Vat and Product. Product has a property of IVat. I
I have two classes ( Test1 and Test2 ) that each have a property
Say I have two classes: Parent and Child . A Parent has a property
Say I have two classes and have a requirement that the primary key property
I have two Entity classes: Order and OrderItem . Order contains a navigation property
I have two classes that each need an instance of each other to function.
I have two classes, one nested in the other. Public Class Operation Public Property
I have two classes, Test2 and Test3. Test2 has an attribute test3 that is
I have two classes in Grails application , Employee has hasMany(one To Many relations)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.