首页 文章

List.Exists在Portable Class Library中缺失

提问于
浏览
0

我正在创建一个可移植类库(PCL),我正在尝试使用List.Exists()和List.TrueForAll(),但我被告知System.Collections.Generic.List不包含Exists或者Exists的定义TrueForAll . 我正在创建的PCL适用于.Net 4.5,Silverlight 4,Windows Phone 7.5,Mono Android和Mono iOS . 有什么东西我错过了吗?

注意:此代码适用于我制作的.Net 4.0库 .

返回错误的代码示例:

List<object> set0;
List<object> set1;

if (set0.TrueForAll(obj0 => set1.Exists(obj1 => obj0.Equals(obj1))))
    return true;

if(!(set0.Exists(obj0 => !set1.Exists(obj1 => obj0.Equals(obj1)))))
    return true;

错误回收:

错误:'System.Collections.Generic.List'不包含'Exists'的定义,也没有扩展方法'Exists'接受类型为'System.Collections.Generic.List'的第一个参数'(你丢失了吗?) using指令或程序集引用?)错误:'System.Collections.Generic.List'不包含'TrueForAll'的定义,也没有扩展方法'TrueForAll'接受类型'System.Collections.Generic.List的第一个参数'可以找到(你错过了使用指令或程序集引用吗?)

1 回答

  • 1

    您似乎试图以麻烦的方式确定 set0set1 的子集(数学上) . 如果您将类型从 List<> 更改为 HashSet<>SortedSet<> ,您将免费获得此功能 .

    否则,请考虑使用

    set0.Except(set1).Any()
    

    来自Linq .

    我不确定可移植类库(PCL)中存在哪些方法,但根据the List<>.Exists doc这种方法可以 . 还有我提到的Linq方法 .

相关问题