I am trying to use Concat() on multiple ISets to make one larger ISet. So I tried the following piece of code:
public class Foo
{
private Dictionary<Bii, ISet<Faa>> items = new Dictionary<Bii, ISet<Faa>>();
public ISet<Faa> GetCompleteList()
{
ISet<Faa> result = items.Values.Aggregate((x,y) => x.Concat(y));
return result;
}
}
The problem is that this results in a Compiler error:
Cannot implicitly convert type
System.Collections.Generic.IEnumerable<Faa>toSystem.Collections.Generic.ISet<Faa>. An explicit conversion exists (are you missing a cast?)
And a second error:
Cannot convert lambda expression to delegate type
System.Func<System.Collections.Generic.ISet<Faa>,System.Collections.Generic.ISet<Faa>,System.Collections.Generic.ISet<Faa>>because some of the return types in the block are not implicitly convertible to the delegate return type
I also tried using a cast like:
ISet<Faa> result = items.Values.Aggregate((x,y) => (ISet<Faa>)x.Concat(y));
But this will give me an InvalidCastException, because it should be a ConcatIterator or some sort.
How can I do a good cast to join all ISets to one ISet?
LINQ functions such as
Concatreturns anIEnumerable. There is noISetanymore after this call. You can rebuild one though:Or, using
SelectManyto simplify: