Not quite sure why you want to go for all of this when linq does most of this for you:
IEnumerable<Person> somePeople; // from wherever somePeople.Where(x => x.CreateDate < new DateTime(2000,1,1)) .ForEach(x => x.PersonClassification = "Online");
Just adding ForEach from here , noting proisos why it is not enabled by default.
If you want to make WhereCreatedBefore better than a simple extension, for example:
static class PersonExtensions { public static bool WhereCreatedBefore(this Person p, int year, int month, int day) { return p.CreateDate < new DateTime(year,month,day); } }
which is useful in itself and gives you:
somePeople.Where(x => x.CreatedBefore(2000,1,1)) .ForEach(x => x.PersonClassification = "Online");
Why limit yourself by simply extending linq tools to make your work easier.
If you want to associate several side effects, a simple ForEach change, for example:
public static IEnumerable<T> Modify<T>( this IEnumerable<T> input, Action<T> action) { foreach (var x in input) { action(x); yield return x; } }
gives you:
somePeople.Where(x => x.CreatedBefore(2000,1,1)) .Modify(x => x.PersonClassification = "Online"); .Modify(x => x.LastModifiedBy = Environment.UserName);
Or if you use the built-in part of the language:
(from p in somePeople where p.CreatedBefore(2000,1,1)) select p) .Modify(p => p.PersonClassification = "Online"); .Modify(p => p.LastModifiedBy = Environment.UserName);
IF you really * wanted you to write a ClassifyAs extension, for example:
public static IEnumerable<Person> ClassifyAs( this IEnumerable<Person> input, string classification) { foreach (var p in input) { p. PersonClassification = classification; yield return p; } }
will provide you the original:
(from p in input where p.CreatedBefore(2000,1,1)) select p).ClassifyAs("Online");
This is one liner! without any bizarre structures or type hierarchies, just some useful extension methods. Linq is usually well designed, well implemented, ubiquitous, and well integrated in C #. Repeated execution of parts of the request would be stupid and wasteful that you want to add a side effect to it that causes operations. This is normal (you have mutable objects, so this is unlikely to cause a problem) just add these operations. Just forcing them to continue to contribute, your code will work in style freely.