我正在尝试在多个 ISet 上使用 Concat()
来创建一个更大的 ISet。所以我尝试了以下代码:
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;
}
}
问题是这会导致编译器错误:
无法将类型 System.Collections.Generic.IEnumerable
隐式转换为 System.Collections.Generic.ISet
。存在显式转换(您是否缺少强制转换?)
以及第二个错误:
无法将 lambda 表达式转换为委托类型System.Func、System.Collections.Generic.ISet、System.Collections.Generic.ISet> ;
因为块中的某些返回类型不能隐式转换为委托返回类型
我也尝试使用类似的强制转换:
ISet<Faa> result = items.Values.Aggregate((x,y) => (ISet<Faa>)x.Concat(y));
但是这个会给我一个 InvalidCastException ,因为它应该是一个 ConcatIterator 或某种类型。
如何才能将所有 ISet 加入到一个 ISet 中?
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>
to System.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?
发布评论
评论(3)
Concat
等 LINQ 函数返回IEnumerable
。此调用之后不再有ISet
。不过,您可以重建一个:或者,使用
SelectMany
来简化:LINQ functions such as
Concat
returns anIEnumerable
. There is noISet
anymore after this call. You can rebuild one though:Or, using
SelectMany
to simplify:你可以尝试这样的事情:
You could try something like this:
如果您不想更改任何传入的集合,您可以执行以下操作:
如果您不想引入具体类型,您可以附加到第一个传入的集合,但是您将更改小于的类型恒星。
If you don't want to change any of the incoming sets you can do something like this:
If you don't want to introduce a concrete type you could append to the first incoming Set but then you would have altered that which is less than stellar.