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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:10:00+00:00 2026-05-15T08:10:00+00:00

I have a list of duplicate numbers: Enumerable.Range(1,3).Select(o => Enumerable.Repeat(o, 3)).SelectMany(o => o) //

  • 0

I have a list of duplicate numbers:

Enumerable.Range(1,3).Select(o => Enumerable.Repeat(o, 3)).SelectMany(o => o)
// {1,1,1,2,2,2,3,3,3}

I group them and get quantity of occurance:

Enumerable.Range(1,3).Select(o => Enumerable.Repeat(o, 3)).SelectMany(o => o)
    .GroupBy(o => o).Select(o => new { Qty = o.Count(), Num = o.Key })

Qty   Num
3     1
3     2
3     3

What I really need is to limit the quantity per group to some number. If the limit is 2 the result for the above grouping would be:

Qty   Num
2     1
1     1
2     2
1     2
2     3
1     3

So, if Qty = 10 and limit is 4, the result is 3 rows (4, 4, 2). The Qty of each number is not equal like in example. The specified Qty limit is the same for whole list (doesn’t differ based on number).

Thanks

  • 1 1 Answer
  • 1 View
  • 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-15T08:10:01+00:00Added an answer on May 15, 2026 at 8:10 am

    There was a similar question that came up recently asking how to do this in SQL – there’s no really elegant solution and unless this is Linq to SQL or Entity Framework (i.e. being translated into a SQL query), I’d really suggest that you not try to solve this problem with Linq and instead write an iterative solution; it’s going to be a great deal more efficient and easier to maintain.

    That said, if you absolutely must use a set-based (“Linq”) method, this is one way you could do it:

    var grouped =
        from n in nums
        group n by n into g
        select new { Num = g.Key, Qty = g.Count() };
    
    int maxPerGroup = 2;
    var portioned =
        from x in grouped
        from i in Enumerable.Range(1, grouped.Max(g => g.Qty))
        where (x.Qty % maxPerGroup) == (i % maxPerGroup)
        let tempQty = (x.Qty / maxPerGroup) == (i / maxPerGroup) ? 
            (x.Qty % maxPerGroup) : maxPerGroup
        select new
        {
            Num = x.Num,
            Qty = (tempQty > 0) ? tempQty : maxPerGroup
        };
    

    Compare with the simpler and faster iterative version:

    foreach (var g in grouped)
    {
        int remaining = g.Qty;
        while (remaining > 0)
        {
            int allotted = Math.Min(remaining, maxPerGroup);
            yield return new MyGroup(g.Num, allotted);
            remaining -= allotted;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: list.clear() vs list = new ArrayList<int>(); I have a list: List<Integer> l1
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
I have a list of dicts that have a property that may be duplicate
Possible Duplicate: Concatenating a C# List of byte[] I have a list of byte[]
Does a List have a property or mechanism by which I can prevent duplicate
Possible Duplicate: Open child browser window from child window I have a list of
Possible Duplicate: Natural Sort Order in C# I have a list with a lot
Possible Duplicate: How to convert List<Integer> to int[] in Java? I have an ArrayList
Possible Duplicate: Python, compute list difference I have two lists For example: A =
So I have a list of numbers in python in a list like this

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.