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.
I don’t see how there should be an infinite loop, but your code is extremely inefficient.
For every item in
rawBomyou 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:
HashSet<string>instead of aList<string>. This changes the call toContainsfromO(n)toO(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.