我想利用IEnumerable<string>
我尝试过:
IEnumerable<string> onlyInFirstSet = xxx.AlphaList.Except(xxx.BetaList); //set where everything in AlphaList, except BetaList
引发编译时错误:
AlphaCollection不包含'Except'的定义,最佳扩展方法重载...
[AlphaList
的类型为AlphaCollection
,这是
public class AlphaCollection: KeyedCollection<string, Alpha>
{
protected override IEnumerable<string> GetKeysForItem(Alpha item)
{
return item.GetKeys(item.Key);
}
}
KeyedCollection
在这里:
public abstract class KeyedCollection<TKey, TItem> : ICollection<TItem>{ ... }
与BetaList
相同-> BetaCollection
-> KeyedCollection
我由此可以创建onlyInFirstSet
,但是我想利用IEnumerable
List<string> onlyInFirstSet = new List<string>();
foreach (Alpha i in xxx.AlphaList)
{
if (!xxx.BetaList.Contains(i.Key))
{
onlyInFirstSet.Add(i.Key);
}
}
[IEnumerable
的任何使用方式或为什么我不能使用它?
由于没有为您的KeyedCollection定义,因此它使用了扩展方法,它们的定义如下:
您必须使用second并定义一个实现IEqualityComparer接口的类以执行比较。
否则,简单地使用Where方法进行过滤会容易得多。
IEnumerable<string> onlyInFirstSet = xxx.AlphaList.Where(i=> !xxx.BetaList.Contains(i));