Do not call ToArray() at the end of your method, just use yield return . So do this:
public static IEnumerable<AnalysisResult> Distinct(AnalysisResult[] results) { var query = results.Distinct(new AnalysisResultDistinctItemComparer()); foreach(AnalysisResult ar in query) {
Basically, yield return uses some compiler magic to ensure that the iteration remains lazy, so you don't have to wait until the complete new collection is created before returning to the caller. Instead, as each item is computed, you immediately return that item to the consumer (which can then execute update logic - for each item, if necessary). You can use the same method in your GetDistinct method.
Jon Skeet has an implementation that looks like this ( LINQ's Distinct () for a specific property ):
public static IEnumerable<TSource> DistinctBy<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) { HashSet<TKey> seenKeys = new HashSet<TKey>(); foreach (TSource element in source) { if (seenKeys.Add(keySelector(element))) { yield return element; } } }
Note that it uses a HashSet , which is built to prohibit duplicates. Just check if the item is added, and if not, return it.
With everything said, remember that this is a question such as "Algorithms and Data." It would be much easier to do something like this:
Dictionary<Key, Value> distinctItems = new Dictionary<Key, Value>(); foreach (var item in nonDistinctSetOfItems) { if (distinctItems.ConatainsKey(item.KeyProperty) == false) { distinctItems.Add(item.KeyProperty, item); } } ... = distinctItems.Values
That is, a character table / Dictionary is created for this problem - linking entries with unique keys. If you save your data this way, it will greatly simplify the problem. Do not forget about the simple solution!