是否在C#中为BlockingCollection提供了任何等效的数据结构<keyvaluepair <string,string>&gt; </keyvaluepair <string,string>

时间:2013-04-28 13:57:06

标签: c# c#-4.0

我正在使用PLinq,我必须为每个PLINQ添加项目集合到共享集合,目前我正在使用BlockingCollection&gt; C#中是否有其他可用的数据结构,它具有类似的功能。我需要让这个集合为给定的密钥保存不同的值。

1 个答案:

答案 0 :(得分:1)

我通过创建List&gt;解决了我的问题与IComparer。

这是Comparer public class KeyValueComparer:IEqualityComparer&gt;     {         public bool Equals(KeyValuePair x,KeyValuePair y)         {             return(x.Key == y.Key&amp;&amp; x.Value == y.Value);         }

    public int GetHashCode(KeyValuePair<string, string> referenceObj)
    {
        //Check whether the object is null 
        if (Object.ReferenceEquals(referenceObj, null)) return 0;

        //Get hash code for the Name field if it is not null. 
        int hashProductName = referenceObj.Equals(default(KeyValuePair<string, string>)) ? 0 : referenceObj.Value.GetHashCode();

        return hashProductName;
    }
}

我试着收集电话  var collection = CollectionWithKVPair.Distinct(new KeyValueComparer());

这将解决具有多个相同键但值不同的列表的问题。