I’m using the following pattern in C#:
IList<foo> x = y.Select(a => new foo { b = Calc1(), c = Calc2() }).ToList(); foreach(foo f in x) { f.d = b / c; }
What I would like to do though is:
IList<foo> x = y.Select(a => new foo { b = Calc1(), c = Calc2() d = b / c; }).ToList();
So the question is: How can you modify this pattern to allow the assignment of a value that is dependent on other values being calculated during the assignment?
(Somebody will probably point out that d should be a property that does the calculation and return a value. This is a contrived example. Assume that the value of d is calculated using other values in addition to c & b which are not available later.)
You can’t re-use initialized properties in an initializer.
I like Erik’s technique. If the query expression syntax is a bother, you can use a full-on anonymous method.