Assigning values inside a LINQ Select?

后端 未结 3 1273
小蘑菇
小蘑菇 2020-12-06 16:36

I have the following query:

drivers.Select(d => { d.id = 0; d.updated = DateTime.Now; return d; }).ToList();

drivers is a List which com

3条回答
  •  -上瘾入骨i
    2020-12-06 16:59

    Although this looks innocent, especially in combination with a ToList call that executes the code immediately, I would definitely stay away from modifying anything as part of a query: the trick is so unusual that it would trip up readers of your program, even experienced ones, especially if they never saw this before.

    There's nothing wrong with foreach loops - the fact that you can do it with LINQ does not mean that you should be doing it.

提交回复
热议问题