-1

Might be simple but am not sure how to implement without foreach

public class Car
{
    public string Model { get; set; }
    public DateTime? LastModified { get; set; }
}

Am having List with 1000 Count

Now i need to set LastModified to Null for all the 1000 Records.

foreach(var item in Car)
{
item.LastModified = null;
}

Without Foreach is there any other better way i can implement ?

Thanks

Peru
  • 2,871
  • 5
  • 37
  • 66
  • 3
    what is wrong with this ? – Selman Genç Aug 06 '14 at 10:09
  • @Selman22 just to know(Leran) if there any other ways i can do this. I cant get a better placer than SO for this :) – Peru Aug 06 '14 at 10:12
  • A `foreach` is about as simple/"best" as they come for this task. If you're doing this often, you can create a static method (or extension method) to do this for you and avoid duplicating the loop throughout your code. – Chris Sinclair Aug 06 '14 at 10:15

2 Answers2

0

As Christos says Linq is one way to achieve this goal, however this answer by Jon Skeet to a similar question explains why you shouldn't use it.

You're not querying here so use the standard foreach loop, possibly with an extension method as Chris Sinclair mentions.

Community
  • 1
  • 1
Underscore
  • 1,017
  • 2
  • 10
  • 26
0

There is also the ForEach method (not a keyword) you can use:

List<Car> cars = new List<Car>();
cars.ForEach((c) => c.LastModified = null);
romanoza
  • 4,775
  • 3
  • 27
  • 44