如何将字典转换为ConcurrentDictionary

如何将字典转换为ConcurrentDictionary

本文介绍了如何将字典转换为ConcurrentDictionary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了如何将 ConcurrentDictionary转换为字典,但我有字典,想转换为ConcurrentDictionary.我该怎么做?...更好的是,我可以将link语句设置为ConcurrentDictionary吗?

I have seen how to convert a ConcurrentDictionary to a Dictionary, but I have a dictionary and would like to convert to a ConcurrentDictionary. How do I do that?... better yet, can i set the link statement to be a ConcurrentDictionary?

var customers = _customerRepo.Query().Select().ToDictionary(x => x.id, x => x);

推荐答案

使用 ConcurrentDictionary<TKey, TValue> Constructor (IEnumerable<KeyValuePair<TKey, TValue>>)构造函数,它可以接受像这样的字典对象:

UseConcurrentDictionary<TKey, TValue> Constructor (IEnumerable<KeyValuePair<TKey, TValue>>) constructor which can accept a dictionary object like:

Dictionary<int, string> dictionary = new Dictionary<int, string>();
dictionary.Add(1,"A");
dictionary.Add(2, "B");

ConcurrentDictionary<int,string> concurrentDictionary =
             new ConcurrentDictionary<int, string>(dictionary);

不. .没有可用于在LINQ中创建ConcurrentDictionary的扩展方法.您可以创建自己的扩展方法,也可以在投影结果时在LINQ查询中使用ConcurrentDictionary构造函数.

No. . There is no extension method available to create ConcurrentDictionary in LINQ. You can either create your own extension method or you can use the ConcurrentDictionary constructor in your LINQ query while projecting results.

这篇关于如何将字典转换为ConcurrentDictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:34