prog_prog prog_prog - 2 months ago 3x
C# Question

Update member in class using LINQ

I have a class with 5 members.
like that:

class Demo
public int id;
public string name;
public string color;
public int 4th_member;
public int 5th_member;

I have list of this class.

for the
, and
, I have 2 list of dictionary with int key and int value. (one for 4th, and the second for 5th)

I want to update these members, according to the dictionary.
like, if dictionary's key = id, then update
to be value of Dictionary.

I hope my question is clear enough.


I tested the below code its working fine.

Hope this will solve your problem if I have understood your question properly

var demo = demoTest.Select(s =>
            s.Fourthth_member = dic.GetValueFromDictonary(s.Fourthth_member);
            s.Fifthth_member = dic1.GetValueFromDictonary(s.Fifthth_member);
            return s;

//Extension method
public static class extMethod
  public static int GetValueFromDictonary(this Dictionary<int, int> dic, int key)
        int value = 0;

        dic.TryGetValue(key, out value);

        return value;