如何将字典转换为ConcurrentDictionary? [英] How do you convert a dictionary to a ConcurrentDictionary?

查看:486
本文介绍了如何将字典转换为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>>)构造函数,它可以接受像这样的字典对象:

Use ConcurrentDictionary<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吗?

can i set the LINQ statement to be a ConcurrentDictionary?

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

No. You can not.. 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆