需要合并两个数据集合的帮助
我需要从两个列表中检索包含给定值的所有项目。
示例:
var list1 = {
new Dummy(){ Name = "Dummy1", Number = 1 },
new Dummy(){ Name = "Dummy2", Number = 2 },
new Dummy(){ Name = "Dummy3", Number = 3 }
};
var list2 = {
new Dummy(){ Name = "Dummy4", Number = 4 },
new Dummy(){ Name = "Dummy5", Number = 2 },
new Dummy(){ Name = "Dummy6", Number = 6 }
};
var list3 = GetAllDummiesWithNumbersContainedInBothLists();
我希望 list3 包含 Dummy2 和 Dummy5,因为两者具有相同的编号。
我该怎么做?应该很简单,但我无法弄清楚......
I need to retrieve all items from two lists that contains a given value.
Example:
var list1 = {
new Dummy(){ Name = "Dummy1", Number = 1 },
new Dummy(){ Name = "Dummy2", Number = 2 },
new Dummy(){ Name = "Dummy3", Number = 3 }
};
var list2 = {
new Dummy(){ Name = "Dummy4", Number = 4 },
new Dummy(){ Name = "Dummy5", Number = 2 },
new Dummy(){ Name = "Dummy6", Number = 6 }
};
var list3 = GetAllDummiesWithNumbersContainedInBothLists();
I want list3 to contain Dummy2 and Dummy5, since both have the same number.
How do i do this? It should be simple but i cant figure it out...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看这是否适合您:
这会将匹配的虚拟对象配对到同一范围内,然后将集合展平,以便您在一个序列中获得所有匹配项。末尾的
Distinct
确保每个虚拟值恰好出现一次,即使任一列表包含重复的数字也是如此。See if this works for you:
This pairs matching dummies into the same scope, then flattens out the set so you get all of the matches in one sequence. The
Distinct
at the end ensures that each dummy appears exactly once even if either list contains repeated numbers.我不完全确定你的要求是什么,但也许是这样的?
如果您能明确具体要求(结果是否需要按
Number
分组、Number
对于列表中的项目是否唯一等),我们可以提供更好的解决方案。I'm not entirely sure what your requirements are, but something like this perhaps?
if you can clarify the exact requirements (do the results need to be grouped by the
Number
, IsNumber
unique for an item within a list etc.), we can provide better solutions.这里还有一个!
Here's one more!