简体   繁体   English

C#更新字典 <string,string> 使用LINQ查询

[英]C# Update a Dictionary<string,string> with LINQ Query

I am trying to update my program settings, which are stored in a Dictionary. 我正在尝试更新存储在词典中的程序设置。 Strangely enough if they were stored in a DatabaseContext the following function would work (I think), but since they are in a Dictionary I am getting a compiler error. 如果将它们存储在DatabaseContext中,那么奇怪的是,以下功能将起作用(我认为),但是由于它们位于Dictionary中,因此出现编译器错误。 Why is my KeyValuePair.Value read only? 为什么我的KeyValuePair.Value是只读的? More importantly, how can I re-write the following function to actually update the Dictionary? 更重要的是,我该如何重写以下函数以实际更新字典?

Thanks 谢谢

public void putSetting(Dictionary<string, string> settings, string setting, string value)
{
  var qry = (from s in settings
             where s.Key == setting
             select s).Single();
   qry.Value = value; // Error  1 Property or indexer 
                      // 'System.Collections.Generic.KeyValuePair<string,string>.Value'
                      // cannot be assigned to -- it is read only
}

Quite honestly I would prefer writing the following: 老实说,我更喜欢写以下内容:

settings[setting] = value;

instead of having this putSetting method which is deprived of any natural sense for existence. 而不是使用这个putSetting方法来剥夺任何自然存在的意义。 I must admit that I have seen many things during code reviews but this one is a winner. 我必须承认,我在代码审查期间看过很多东西 ,但这是赢家。

Not only is it correct that the Value is readonly, you also should consider that the KeyValuePair itself is a struct - so the KeyValuePair you have after your query is a new instance, not a reference to the one in the Dictionary . Value是只读的是正确的,还应该考虑KeyValuePair本身是一个结构 -因此查询后拥有的KeyValuePair是一个新实例,而不是对Dictionary实例的引用。


I'm a bit confused why you are using linq for this? 我有点困惑,为什么您要使用linq?

For a Dictionary<string,string> you can just use: 对于Dictionary<string,string>您可以使用:

 settings[setting] = value

The Value of a KeyValuePair is always readonly. Value一的KeyValuePair始终是只读的。 Note the decleration 注意脱皮

public TValue Value { get; }

Use 采用

settings[setting] = value;

instead. 代替。 Seeing how short that is, I'm not sure what you're really gaining with this method. 看到这么短,我不确定您通过这种方法真正获得了什么。

There are a couple of problems with this approach. 这种方法存在两个问题。

  • The type of qry is a KeyValuePair<TKey, TValue> . qry的类型是KeyValuePair<TKey, TValue> This is a readonly struct which means you can't mutate it's properties. 这是一个只读结构,这意味着您不能对其属性进行突变。
  • Additionally since it's a struct it's a copy of the struct inside the Dictionary<TKey, TValue> . 另外,由于它是一个结构,因此它是Dictionary<TKey, TValue>中的结构的副本。 Even if you could mutate it it wouldn't update the value inside the Dictionary<TKey, TValue> and hence wouldn't have an effect 即使您可以对其进行突变,也不会更新Dictionary<TKey, TValue> ,因此不会产生任何影响

What you're looking for here is the indexer on the Dictionary<TKey, TValue> which allows for inserts of values for a given key 您在这里寻找的是Dictionary<TKey, TValue>上的索引器Dictionary<TKey, TValue>它允许为给定键插入值

settings[setting] = value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM