update value in list c#

41

find a value in list of objects in c# -

var item = TextPool.FirstOrDefault(o => o.Name == "test");
if (item != null)
       item.value = "Value";

update value in list c# -

foreach (var mc in list.Where(x => x.Name == "height"))  
     mc.Value = 30;

update value in list c# -

var result = list.Select(i => 
   { 
      if (i.Name == "height") i.Value = 30;
      return i; 
   }).ToList();

update value in list c# -

list.Where(w => w.Name == "height").ToList().ForEach(s => s.Value = 30);

update value in list c# -

list = list.Where(c=>c.Name == "height")
        .Select( new t(){Name = c.Name, Value = 30})
        .Union(list.Where(c=> c.Name != "height"))
        .ToList();

update value in list c# -

public static IEnumerable<MyClass> SetHeights(
    this IEnumerable<MyClass> source, int value)
{
    foreach (var item in source)
    {
       if (item.Name == "height")
       {
           item.Value = value;
       }

       yield return item;
    } 
}

var result = list.SetHeights(30).ToList();

Comments

Submit
0 Comments