how to change the value of dictionary using linq based on key?

17,345

Solution 1

LIN(Q) is a tool to query something not to update it. However, you can first query what you need to update. For example:

var toUpdate = customList
   .Where(c => newdictionary.ContainsKey(c.SerialNo))
   .Select(c => new KeyValuePair<string, string>(c.SerialNo, c.ecoID.ToString()));
foreach(var kv in toUpdate)
    newdictionary[kv.Key] = kv.Value;

By the way, you get the "KeyValuePair.Value' cannot be assigned to it is read only" exception because aKeyValuePair<TKey, TValue> is a struct which cannot be modified.

Solution 2

You'd have the simplest in this form: though I don't see why you are assigning the same value but the method applies regardless

 var dictionary = new Dictionary<string, string>() { { "12345", "chip1" }, { "23456", "chip2" } };
                var customList = new List<CustomSerial>() { new CustomSerial() { ecoID = 1, SerialNo = "12345" }, new CustomSerial() { ecoID = 2, SerialNo = "23456" } };

                dictionary.Keys.ToList().ForEach(key =>
                {
                    dictionary[key] = customList.FirstOrDefault(c => c.SerialNo == key).SerialNo;
                });
Share:
17,345
Sajeetharan
Author by

Sajeetharan

👋 Follow and say Hi @Kokkisajee . Click here To know more about me

Updated on June 15, 2022

Comments

  • Sajeetharan
    Sajeetharan almost 2 years

    I have a Dictionary which is of type,

      Dictionary<string, string> newdictionary = new Dictionary<string, string>();
      newdictionary.Add("12345", "chip1");
      newdictionary.Add("23456", "chip2");
    

    Now i have a List which is of type

       internal class CustomSerial
        {
            public string SerialNo { get; set; }
            public decimal ecoID { get; set; }
        } 
       var customList = new List<CustomSerial>();
       CustomSerial custObj1= new CustomSerial();
       custObj1.ecoID =1;
       custObj1.SerialNo = "12345";
       customList.Add(custObj1);
       CustomSerial custObj2 = new CustomSerial();
       custObj2.ecoID = 2;
       custObj2.SerialNo = "23456";
       customList.Add(custObj2);
    

    Now i need to update the Initial dictionary by Filtering the Keys with ther SerialNumber and Replacing the values with the ecoID.

    When i try this, it gives

      foreach (KeyValuePair<string, string> each in newdictionary)
      {                       
        each.Value = customList.Where(t => t.SerialNo == each.Key).Select(t => t.ecoID).ToString();
      }
    

    System.Collections.Generic.KeyValuePair.Value' cannot be assigned to -- it is read only

  • Sajeetharan
    Sajeetharan almost 10 years
    is toUpdate is a dictionary?
  • Tim Schmelter
    Tim Schmelter almost 10 years
    @Sajeetharan: it's an IEnumerable<KeyValuePair<string, string>>.
  • Sajeetharan
    Sajeetharan almost 10 years
    Thanks! will mark it as an answer after testing it :)
  • Sajeetharan
    Sajeetharan almost 10 years
    Instead of updating it's adding a new key and value
  • Tim Schmelter
    Tim Schmelter almost 10 years
    @Sajeetharan: no, i'm first taking only key-values which are already in the dictionary: ...Where(c => newdictionary.ContainsKey(c.SerialNo))
  • Sajeetharan
    Sajeetharan almost 10 years
    is it possible for you to tell how can i update the Serialno with the ecoId Having chipids in values
  • Tim Schmelter
    Tim Schmelter almost 10 years
    @Sajeetharan: i don't understand the question. Do you want to update the key of the dictionary? That's not possible, you need to create a new one.