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 6386141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:59:13+00:00 2026-05-25T02:59:13+00:00

The problem is: I got a list of elements and then I use group

  • 0

The problem is: I got a list of elements and then I use group by. I need to get another list where contains the half of elements from each group by.

How can I do that? I’m using LINQ.

UPDATE:

This is the first list that I get.

        XDocument xdoc = XDocument.Load(path);
        var conditions = from c in xdoc.Descendants("Condition")
                         select new
                         {
                             ObjectiveID = (int)c.Attribute("ObjectiveID"),
                             TypeID = (int)c.Attribute("TypeID"),
                             ProblemID = (int)c.Attribute("ProblemID"),
                             Ranges = (from r in c.Descendants("Range")
                                       select new
                                       {
                                           Decimals = (int)r.Attribute("Decimals"),
                                           Min = (decimal)r.Attribute("Min"),
                                           Max = (decimal)r.Attribute("Max")
                                       }).ToArray(),
                         };

That’s the original which I’m using. From that one, I only want to get the half of problems from each OBJECTIVEID.

If in the enummerable I have 2 elements of the same objectiveID, I must get only one. If I got one problem, I must get only one, If I have 5 I’d have 2 or 3.

  • 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-25T02:59:13+00:00Added an answer on May 25, 2026 at 2:59 am

    I’m not sure what you’re asking — are you trying to get individual elements from each group into another list? If so, SelectMany is probably what you’re looking for.

    var numbers = new[] { 1,2,3,4,5,6,7,8,9 };
    var evensAndOdds = numbers.GroupBy(x => x % 2);
    var evens = evensAndOdds.Where(g => g.Key == 0).SelectMany(g => g).ToList();
    var odds =  evensAndOdds.Where(g => g.Key == 1).SelectMany(g => g).ToList();
    

    Alternatively:

    var evens = evensAndOdds.Single(g => g.Key == 0).ToList();
    

    Response to Edit

    There’s an overload of Select that also includes an integer index — you can use this to filter out all the odd or even items to get half of the set.

    You could change it to something like

    Ranges = c.Descendants("Range")
              .Select((range,i) => new { range, i })
              .Where(pair => pair.i % 2 == 0) // select only even items
              .Select(pair => new {
                   Decimals = (int)pair.range.Attribute("Decimals"),
                   ... etc...
              })
              .ToArray()
    

    I’m beginning to think I don’t understand the question. If the problem is that you have data like

    condition1: objectiveID = 2 problemID = 100
    condition2: objectiveID = 2 problemID = 101
    

    and you don’t want two different problemIDs for the same objectiveID, you can use GroupBy / SelectMany / Take to narrow down to only one problem per objectiveID

    xdoc.Descendants("Condition")
        .GroupBy(c => c.Attribute("objectiveID").value)
        .SelectMany(group => group.Take(1))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a double linked list with many list elements. so i can use
I got a problem like this (this is html/css menu): Eshop | Another eshop
I got a problem with this linq query: from PersistedFileInfo fi in m_Database from
I got a problem today. It had a method and I need to find
I've got a problem with a CKEditor plugin. I need PHP code to make
I have got this problem: Find the first element in a list, for which
I've got a problem with those data, my json array data contains: {list_file:[{\id\:\511\,\name\:\Jellyfish.jpg\,\projectId\:\12539\,\projectName\:\project namessszd
I've got this problem and I can't for the life of me find a
I'm learning queues from a book. I've got into a problem while learning circular
I am trying to remove comments from a list of elements that were obtained

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.