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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:00:09+00:00 2026-05-27T02:00:09+00:00

How can I control the loop of Tuple repetition? Someone has given me a

  • 0

How can I control the loop of Tuple repetition?

Someone has given me a hint about an algorithm, which I have modified a little.

int LimCol = Convert.ToInt32(LimitColis);

result = oListTUP
         .GroupBy(x => x.Item1)
         .Select(g => new
         {
             Key = g.Key,
             Sum = g.Sum(x => x.Item2),
             Poids = g.Sum(x => x.Item3),
         })
         .Select(p => new
         {
             Key = p.Key,
             Items = Enumerable.Repeat(LimCol , p.Sum  / LimCol).Concat(Enumerable.Repeat(p.Sum  % LimCol, 1)),
             CalculPoids = p.Poids / (Enumerable.Repeat(LimCol, p.Sum / LimCol).Concat(Enumerable.Repeat(p.Sum % LimCol, 1))).Count()
         })
         .SelectMany(p => p.Items.Select(i => Tuple.Create(p.Key, i, p.CalculPoids)))
                        .ToList();

foreach (var oItem in result)
{
     Label1.Text += oItem.Item1 + "--" + oItem.Item2 + "--" + oItem.Item3 + "<br>";
}

the result with LimCol = 3
ScreenShot

As you can see, I colored the problem with red.

PS: I have created some Tuple for not using data from SQL

 var list = new List<Tuple<string, int, decimal>>
        {
            Tuple.Create("0452632", 12, 15.0m),
            Tuple.Create("essai 49", 1, 45.0m),
            Tuple.Create("essai 49", 1, 45.0m),
            Tuple.Create("essai 49", 2, 45.0m),
            Tuple.Create("essai 49", 1, 23.0m),


        };

        int TheLimCol = 3;

        var Theresult = list
            .GroupBy(x => x.Item1)
            .Select(g => new
            {
                Key = g.Key,
                Sum = g.Sum(x => x.Item2),
                Poids = g.Sum(x => x.Item3),
            })
            .Select(p => new
            {
                Key = p.Key,
                Items = Enumerable.Repeat(TheLimCol, p.Sum / TheLimCol).Concat(Enumerable.Repeat(p.Sum % TheLimCol, 1)),

                CalculPoids = p.Poids / (Enumerable.Repeat(TheLimCol, p.Sum / TheLimCol).Concat(Enumerable.Repeat(p.Sum % TheLimCol, 1))).Count()

            })
            .SelectMany(p => p.Items.Select(i => Tuple.Create(p.Key, i, p.CalculPoids)))
            .ToList();

        foreach (var oItem in Theresult)
        {
            Label1.Text += oItem.Item1 + "--" + oItem.Item2 + "--" + oItem.Item3 + "<br>";
        }

Actual output:

0452632--3--3,0
0452632--3--3,0
0452632--3--3,0
0452632--3--3,0
0452632--0--3,0
essai 49--3--79,0
essai 49--2--79,0

expected result:

0452632--3--3,75
0452632--3--3,75
0452632--3--3,75
0452632--3--3,75
essai 49--3--79,00
essai 49--2--79,00
  • 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-27T02:00:10+00:00Added an answer on May 27, 2026 at 2:00 am

    The problem is in your “.Concat(Enumerable.Repeat(p.Sum % TheLimCol, 1)”. You’re adding an element even if the remainder (p.Sum % TheLimCol) is equal to 0.

    This should work:

            var result = list
                .GroupBy(x => x.Item1)
                .Select(g => new
                {
                    Key = g.Key,
                    Sum = g.Sum(x => x.Item2),
                    Poids = g.Sum(x => x.Item3),
                })
                .Select(p => new
                {
                    Key = p.Key,
                    Items = Enumerable.Repeat(limCol, p.Sum / limCol).Concat(Enumerable.Repeat(p.Sum % limCol, p.Sum % limCol > 0 ? 1 : 0)),
                    CalculPoids = p.Poids / Enumerable.Repeat(limCol, p.Sum / limCol).Concat(Enumerable.Repeat(p.Sum % limCol, 1)).Count()
                })
                .SelectMany(p => p.Items.Select(i => Tuple.Create(p.Key, i, p.CalculPoids)))
                .ToList();
    

    In this specific case, I would recommend using the alternative syntax of Linq (or not using Linq at all):

            var query = from x in list
                        group x by x.Item1 into g
                        let sum = g.Sum(x => x.Item2)
                        let poids = g.Sum(x => x.Item3)
                        let remainder = sum % limCol
                        let items = Enumerable.Repeat(limCol, sum / limCol).Concat(Enumerable.Repeat(remainder, remainder > 0 ? 1 : 0))
                        select new
                        {
                            Key = g.Key,
                            Items = items,
                            CalculPoids = poids / items.Count()
                        };
    
    
            var result = query.SelectMany(p => p.Items.Select(i => Tuple.Create(p.Key, i, p.CalculPoids)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view user control that can post form. This control can be
I have a user control where the XAML of the control can bind to
I've been wondering about alternative ways to write control structures like you can write
I have one loop for (chr in paste('chr',c(seq(1,22),'X','Y'),sep='')){ . . pdf ('anna.pdf',paper='a4') plot(exp,control,xlim=c(0,400),ylim=c(0,400),pch=20,col='black',main='Tiles',xlab='exp',ylab 'Control')
I have a Control that can overlay multiple C# user controls in my GUI.
I have used Observer Pattern for my application. I have a subject which has
I created a widget/control that I can reuse which I created by extending RelativeLayout
I have a webform which dynamically loads a web user control in it. Within
I have a for loop in which I placed several if statements. The objective
I have a user control with 5 simple radiobuttons, I need ot loop through

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.