You should not be storing related items in separate sequences; that's an antipattern. However, if this happens to be the way that your data is available, you can first combine the items from the sequences using Zip
, then use Distinct
to get unique values.
var result = accounts.Zip(projects, (a, p) => a + "-" + p)
.Zip(subprojects, (a, s) => a + "-" + s)
.Distinct()
.ToList();
Edit: In the spirit of Servy's answer, here is an extension method for zipping an arbitrary number of sequences:
public static partial class EnumerableExtensions
{
public static IEnumerable<TResult> Zip<TSource, TResult>(
this IEnumerable<IEnumerable<TSource>> sequences,
Func<IEnumerable<TSource>, TResult> resultSelector)
{
var enumerators = sequences.Select(sequence => sequence.GetEnumerator()).ToArray();
while (enumerators.All(enumerator => enumerator.MoveNext()))
yield return resultSelector(enumerators.Select(enumerator => enumerator.Current));
}
}
And this is how it would be consumed:
string[][] sequences = { accounts, projects, subprojects };
var results = sequences.Zip(items => string.Join("-", items))
.Distinct()
.ToList();