我正在使用PLinq,我必须为每个PLINQ添加项目集合到共享集合,目前我正在使用BlockingCollection> C#中是否有其他可用的数据结构,它具有类似的功能。我需要让这个集合为给定的密钥保存不同的值。
答案 0 :(得分:1)
我通过创建List>解决了我的问题与IComparer。
这是Comparer public class KeyValueComparer:IEqualityComparer> { public bool Equals(KeyValuePair x,KeyValuePair y) { return(x.Key == y.Key&& 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());
这将解决具有多个相同键但值不同的列表的问题。