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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:05:39+00:00 2026-06-18T12:05:39+00:00

I have a simple class: public class RawBomItem { private string material; private string

  • 0

I have a simple class:

public class RawBomItem
{
    private string material;
    private string item;
    private string component;
    private string quantity;
    private string b;
    private string spt;
...
}

and for every datamember there is a property.

And then I have a List containing the instances of this class

    private List<RawBomItem> rawBom;

The List contains more than 70000 items.

At this point I wanted to run a little complex LINQ query on this List.

List<string> endProducts = new List<string>(
    rawBom.Where(x1 => new List<string>(rawBom.Select(x2 => x2.Component)
                                              .Distinct())
                           .Contains(x1.Material) && (x1.B != "F"))
          .Select(x3 => x3.Material));

The query seems like it run into an infinite loop. (I’ve waited for some minutes before shut it down)

I will turn it into DB to work, I’m just interested in what can be the problem.

  • 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-06-18T12:05:40+00:00Added an answer on June 18, 2026 at 12:05 pm

    I don’t see how there should be an infinite loop, but your code is extremely inefficient.
    For every item in rawBom you calculate the distinct set of the components and copy them into a new list. So, with 70,000 items in your list, you perform 70.000 ^ 2 = 4,900,000,000 iterations. Furthermore, for every item in your list, you are iterating the list of distinct components yet again. Depending on how many distinct components you have, you add the same number of iterations on top.

    This can be improved:

    var components = new HashSet<string>(rawBom.Select(x => x.Component).Distinct());
    var endProducts = rawBom.Where(x => components.Contains(x.Material) &&
                                        x.B != "F")
                            .Select(x => x.Material)
                            .ToList();
    
    1. We extract the creation of the list of distinct components from the main query, so we have to calculate it only once – and not 70.000 times.
    2. We use a HashSet<string> instead of a List<string>. This changes the call to Contains from O(n) to O(1).

    The end result is that you enumerate your list only twice, resulting in only 140,000 iterations. Now compare that to the original number of iterations.

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

Sidebar

Related Questions

I have a simple class CustomQuoteRequest: public class CustomQuoteRequest { private String requestId; private
I have simple type Question : public class Question { public string[] Tags {
I have a simple class: public class site { public string URL { get;
Lets say we have a simple class public class Foo { public string FooName;
I have a simple class that looks like this: public class Item { //
I have a simple class XmlFileHelper as follows: public class XmlFileHelper { #region Private
I have a simple class something like: public class ValueProvider { public void setExpression(String
i have this simple class: public class JDEItemLotAvailability { public string Code { get;
I have this simple class: public class LuceneUtil{ private final EntityManager entityManager; public LuceneUtil()
I have a simple (obfuscated) class: public Thing () { public IList<string> Strings() {

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.