"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!
While you can use a ForEach extension method, if you want to use just the framework you can do.
While you can use a ForEach extension method, if you want to use just the framework you can do collection. Select(c => {c. PropertyToSet = value; return c;}).ToList(); The ToList is needed in order to evaluate the select immediately due to lazy evaluation.
1 I upvoted this because it's a pretty nice solution... the only reason I like the extension method is that it makes it a little clearer to understand exactly what is going on... however your solution is still pretty sweet – lomaxx Dec 30 '08 at 0:07 I agree, my answer is mainly for those who don't want to use the extension method. – Cameron MacFarland Dec 30 '08 at 4:48 If we are creating list anyway, can't we just type: collection.ToList(). ForEach(c => c.
PropertyToSet = value); – Karol Kolenda Feb 12 '10 at 9:53 If collection was an ObservableCollection say, then changing items in place rather than creating a new list can be useful. – Cameron MacFarland Mar 5 '10 at 22:44 1 this is a great tip, +1 – adrianos Jun 6 '11 at 9:56.
I actually found an extension method that will do what I want nicely public static IEnumerable ForEach( this IEnumerable source, Action act) { foreach (T element in source) act(element); return source; }.
1 nice :) Lomaxx, maybe add an example so peeps can see it in 'action' (boom tish! ). – Pure.
Krome Apr 10 '09 at 10:51.
There is no built-in extension method to do this. Although defining one is fairly straight forward. At the bottom of the post is a method I defined called Iterate.It can be used like so collection.
Iterate(c => { c. PropertyToSet = value;} ); Iterate Source public static void Iterate(this IEnumerable enumerable, Action callback) { if (enumerable == null) { throw new ArgumentNullException("enumerable"); } IterateHelper(enumerable, (x, i) => callback(x)); } public static void Iterate(this IEnumerable enumerable, Action callback) { if (enumerable == null) { throw new ArgumentNullException("enumerable"); } IterateHelper(enumerable, callback); } private static void IterateHelper(this IEnumerable enumerable, Action callback) { int count = 0; foreach (var cur in enumerable) { callback(cur, count); count++; } }.
– AnthonyWJones Dec 29 '08 at 22:32 this is pretty close to what I want but a little.. involved. The blog post I posted has a similar implementation but with fewer lines of code. – lomaxx Dec 29 '08 at 22:57 The IterateHelper seems overkill.
The overload that doesn't take an index ends up doing alot more extra work (convert callback to lambda which takes index, keep a count which is never used). I understand it's reuse, but it's a workaround for just using a forloop anyway so it should be efficient. – Cameron MacFarland Dec 29 '08 at 23:20 @Cameron, IterateHelper serves 2 purposes.
1) Single implementation and 2) allows for ArgumentNullException to be thrown at call time vs. use. C# iterators are delayed executed, having the helper prevents the odd behavior of an exception being thrown during iteration. – JaredPar Dec 30 '08 at 3:42 @JaredPar: Except you're not using an iterator.
There's no yield statement. – Cameron MacFarland Dec 30 '08 at 7:27.
No, LINQ doesn't support a manner of mass updating. The only shorter way would be to use a ForEach extension method - stackoverflow.com/questions/101265/why-i....
My 2 pennies:- collection. Count(v => (v. PropertyToUpdate = newValue) == null).
I like the thinking, but it's not really clear what the code is doing – lomaxx Dec 29 '08 at 22:43 Yeah I'm not sure I'd want this in production code. – Cameron MacFarland Dec 29 '08 at 23:18.
You can use Magiq (magiq.codeplex.com), a batch operation framework for linq. Bye!
I've tried a few variations on this, and I keep going back to this guy's solution. hookedonlinq.com/UpdateOperator.ashx Again, this is somebody else's solution. But I've compiled the code into a small library, and use it fairly regularly.
I'm going to paste his code here, for the off chance that his site(blog) ceases to exist at some point in the future. (There's nothing worse than seeing a post that says "Here is the exact answer you need", Click, and Dead URL. ) public static class UpdateExtensions { public delegate void Func(TArg0 element); /// /// Executes an Update statement block on all elements in an IEnumerable sequence.
/// /// The source element type. /// The source sequence. /// The update statement to execute for each element.
/// The numer of records affected. Public static int Update(this IEnumerable source, Func update) { if (source == null) throw new ArgumentNullException("source"); if (update == null) throw new ArgumentNullException("update"); if (typeof(TSource). IsValueType) throw new NotSupportedException("value type elements are not supported by update."); int count = 0; foreach (TSource element in source) { update(element); count++; } return count; } } int count = drawingObjects .
Where(d => d. IsSelected && d. Color == Colors.
Blue) . Update(e => { e. Color = Color.
Red; e. Selected = false; } ).
I assume you want to change values inside a query so you could write a function for it void DoStuff() { Func test = (y, x) => { x. Bar = y; return true; }; List mylist = new List(); var v = from x in mylist where test("value", x) select x; } class Foo { string Bar { get; set; } } But not shure if this is what you mean.
This is going in sort of the right direction by would require something to enumerate v, else it'll do nothing. – AnthonyWJones Dec 29 '08 at 22:35.
You can use LINQ to convert your collection to an array and then invoke Array.ForEach(): Array. ForEach(MyCollection.ToArray(), item=>item.DoSomeStuff()); Obviously this will not work with collections of structs or inbuilt types like integers or strings.
Here is the extension method I use... /// /// Executes an Update statement block on all elements in an IEnumerable of T /// sequence. /// /// The source element type. /// The source sequence.
/// The action method to execute for each element. /// The number of records affected. Public static int Update(this IEnumerable source, Func action) { if (source == null) throw new ArgumentNullException("source"); if (action == null) throw new ArgumentNullException("action"); if (typeof (TSource).
IsValueType) throw new NotSupportedException("value type elements are not supported by update. "); var count = 0; foreach (var element in source) { action(element); count++; } return count; }.
Nothing interferes that! – abatishchev Aug 17 '10 at 14:13 That was specific to the project I was working on. I suppose it wouldn't matter in most cases.
Lately I've reworked that and renamed it Run(...), removed the value type thing and changed it to return void and dropped the count code. – Bill Forney Oct 22 '10 at 4:44 That aligns it with Reactive extensions Run method... – Bill Forney Oct 22 '10 at 4:45.
I am doing this Collection. All(c => { c. NeedsChange = value; return true; }).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.