Using LINQ to Update A Property in a List of Entities

前端 未结 5 1049
耶瑟儿~
耶瑟儿~ 2021-01-31 13:58

Say I have an entity that looks something like this simple example:

MyEntity 
{
   int property1;
   int property2;
   int property3;
}

Now ass

5条回答
  •  粉色の甜心
    2021-01-31 14:11

    To Darin's point, LINQ is built for querying. I've gotten into the mindset when working with LINQ that objects being queried should be treated as immutable. However, the Select operator is also great for transformations.

    So, you could solve your problem by transforming from one set to another as follows:

    var result = myEntityCollection.Select(e => {
       var ret = e;
       e.property1 = 100;
       return e; });
    

    The items in the original collection are untouched, but the items in result will now all have property1 set to 100.

提交回复
热议问题