如何巧妙地查询相应的对象数组项?

编程入门 行业动态 更新时间:2024-10-08 02:28:28
本文介绍了如何巧妙地查询相应的对象数组项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

如何巧妙地查询相应的对象数组项?

本文发布于:2023-11-11 08:33:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577898.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   对象   巧妙地

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!