Change some value inside the List

前端 未结 7 1371
甜味超标
甜味超标 2020-12-07 22:23

I have some list (where T is a custom class, and class has some properties). I would like to know how to change one or more values inside of it by using Lambda Expressions,

7条回答
  •  再見小時候
    2020-12-07 22:44

    This is the way I would do it : saying that "list" is a > where t is a class with a Name and a Value field; but of course you can do it with any other class type.

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

    This works perfectly ! It's a simple linq expression without any loop logic. The only thing you should be aware is that the order of the lines in the result dataset will be different from the order you had in the source dataset. So if sorting is important to you, just reproduce the same order by clauses in the final linq query.

提交回复
热议问题