I have an array of objects that'll be used for some process.
var x = new List<MyObject>() { new MyObject(), new MyObject(), ... }.ToArray();
After the process, it returns an array of results corresponding to each of the input objects.
var y = MyProcess(x); // returns List<MyResult>
Now, my question is how can I query a list of results with a given condition and the corresponding input objects?
For example, if y[2].IsOkay
is false
, I want to obtain both x[2]
and y[2]
in another array.
And the objects in array x and y don't have a reference key except the index.
Well, you can use Zip
to pair them up:
var pairs = x.Zip(y, (a, b) => new { a, b })
.Where(pair => !pair.b.IsOkay)
.ToArray();
You can change the delegate passed to Zip
to compose the two values differently if you want - for example, using a named type instead of the anonymous type I've got above.
See more on this question at Stackoverflow