Perform modified updates in a LINQ query?

I have a LINQ query where I want to return changed objects. If I were in a constant functional mood, I could do something like copy-construtor-like, for example:

from widget in widgets select new widget { legs = widget.legs + 1, arms = widget.arms }

Unfortunately, I am doing this on an NHibernate entity object being modified, and I need to modify the original object. I am looking for some syntax with a little anonymous method with side effects, for example:

from widget in widgets select { widget.legs += 1; return widget }

(with apologies for Scala syntax)

Now I can do this update outside of the LINQ query, but I would prefer to do it inline if possible. Is it possible to insert operations voidsuch as in LINQ?

+3
source share
1 answer
widgets
.ToList()
.Select(widget => 
{
  widget.legs +=1;
  return widget
})
.ToList()

ToList () .

, ToList, Enumerable.Select Queryable.Select.

, , NHibernate .... , , .

from old in widgets
select new widget() {legs = old.legs + 1, arms = old.arms}

, 2 .

+2

Source: https://habr.com/ru/post/1718548/


All Articles