如何巧妙地查询相应的对象数组项? [英] How to neatly query corresponding object array items?

查看:96
本文介绍了如何巧妙地查询相应的对象数组项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个会被用于一些工艺对象的数组

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?

例如,如果 Y [2] .IsOkay ,我想同时获得 X [2] Y [2] 在另一个数组。
和数组x和y的对象没有除指数的参考项。

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.

这篇关于如何巧妙地查询相应的对象数组项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆