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

  • Home
  • SEARCH
  • 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 6564831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:01:03+00:00 2026-05-25T14:01:03+00:00

I have a list list with items in it like this ElementA: Number=1, Version=1

  • 0

I have a list list with items in it like this

ElementA: Number=1, Version=1
ElementB: Number=1, Version=2 
ElementC: Number=1, Version=3 <-
ElementD: Number=2, Version=1
ElementE: Number=2, Version=2 <-
ElementF: Number=3, Version=1 <-

and I want to select all items which have the highest Version in a group with items of same Number. (See the arrows above.)

I would like to use a query for this so I’ve tried this:

var result = from a in list where a.Version >=
            (from b in list where b.Number == a.Number && b != a select b.Version).Max() select a;

This is working fine, if every group of items with the same Number consists of at least 2 elements, but otherwise it throws an InvalidOperationException if the inner query contains no elements.

How can I rewrite the query to get what I want? I would be grateful for every hint. 🙂

  • 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-25T14:01:04+00:00Added an answer on May 25, 2026 at 2:01 pm

    this should do the trick, but without copying your data myself I cannot test it:

    var result = 
         list.GroupBy(x => x.Number)
             .Select(gr => 
                        gr.OrderByDescending(x => x.Version)
                          .First());
    

    as Henrik noticed this will only get you one item – I didn’t see the reason to get more because the items seemd only to consist of the two fields, but this is easily handeled – just add another group for the version:

    var result = 
         list.GroupBy(x => x.Number)
             .Select(gr =>
                        gr.GroupBy(x => x.Version)
                          .OrderByDescending(gr2 => gr2.Key)
                          .Select(gr => gr.ToArray()));
    

    This should result in a enumeration of Arrays with the largest versions – but again I cannot test so there might be syntax or semantic errors but the indent should be clear and errors easy to remove.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of items like this... <ol> <li> <span></span> <img src="image.png" />
I have a String List with items like this Root Root/Item1 Root/Item2 Root/Item3/SubItem1 Root/Item3/SubItem2
I have a visual list of items like this: http://jsfiddle.net/viatropos/XCe3T/1/ In the real app,
If I have a list like this: <ul id=mylist> <li id=list-item1>text 1</li> <li id=list-item2>text
I have a simple list like this: <ul id=cyclelist> <li>Item 1</li> <li>Item 2</li> <li>Item
I have a nested list like this: <ul class=list> <li class=list_item_type_1> <ul class=list> <li
I have a list of items like how its listed below, and I'm trying
On my rails app I have a list of items (like a task list)
I have a list of objects (Move's in this case) that I want to
I have an unordered list of 50 items. I want to show only 10

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.