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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:41:13+00:00 2026-05-20T15:41:13+00:00

I have to be able to sort out a list of object which might

  • 0

I have to be able to sort out a list of object which might contains doubloon with certain criteria. So far the code is working but it takes 10 minutes for 50,000 rows in my list.

Here is the code :

    public class TestObject
    {
        public string value1;
        public string value2;
        public string value3;
        public string value4;

        public int num1;
        public int num2;
    }

    public static List<TestObject> ReturnTestObjectListWithoutDoubloon(List<TestObject> source)
    {
        var destination = new List<TestObject>();
        var list = new Dictionary<int, TestObject>();


        while (source.Count > 0)
        {
            list.Clear();
            var originalElement = source[0];

            foreach (var query in source.Select((element, index) => new { Value = element, Index = index })
            .Where(currentElement => (currentElement.Value.value1 == originalElement.value1)
                                    && (currentElement.Value.value2 == originalElement.value2)
                                    && (currentElement.Value.value3 == originalElement.value3)
                                    && (currentElement.Value.value4 == originalElement.value4)))
            {
                list.Add(query.Index, query.Value);
            }

            if (list.Count > 1)
            {
                originalElement.num1 = list.Sum(a => a.Value.num1);
                originalElement.num2 = list.Sum(a => a.Value.num2);
            }

            destination.Add(originalElement);
            foreach (var positionToremove in list.Keys)
                source.RemoveAt(positionToremove);
        }

        return destination;
    }

The idea is to reduce the list each time I pass the while loop so my Linq request is executed on the smallest list possible. However the fewer doubloons I have , the slower it is. I am looking for a solution which allows me to have the smallest rutnime possible, memory is not an issue.

Does anyone have an idea ?

  • 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-20T15:41:14+00:00Added an answer on May 20, 2026 at 3:41 pm

    I’ve tried to follow your code through – and it looks like you are simply looking to remove duplicates from your source list?

    If that is the case:

    • then I think your source.RemoveAt code might be broken as it might remove the wrong elements.

    • then you should be able to run a single GroupBy() operation on this source list – that should work using a hashtable which should be much quicker than your existing loops-inside-loops operation.

      var query = from s in source
                  group s by new {
                      value1 = s.value1,
                      value2 = s.value2,
                      value3 = s.value3,
                      value4 = s.value4
                  } into grouped
                  select new TestObject()
                  {
                      value1 = grouped.Key.value1,
                      value2 = grouped.Key.value2,
                      value3 = grouped.Key.value3,
                      value4 = grouped.Key.value4,
                      num1 = grouped.Sum(x => x.num1),
                      num2 = grouped.Sum(x => x.num2)
                  };
      return query.ToList();
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple question, not able to sort it out. I have a
I have a dojo.dnd Source object that I just want to list out normally,
I have main categories and sub categories. I want to be able to sort
I have been able to find lots of examples of adding a Dropdown list
Ok, here's the problem: I have a list of contacts, which i have created
I want to create a list of those files in a directory which have
I have a list of Person s each which have multiple fields that I
I currently have a DataGridView bound to a LIST that contains 4 fields. The
I have a View that is displaying data from a list of object that
Okay I have spent the last 2 days trying to sort this one out.

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.