C#LINQ工会问题 [英] C# linq union question
问题描述
有人能解释LINQ如何做联盟
工作?
Could someone explain how does Union
in LINQ work?
据告知,合并两个序列的删除重复的
It is told that it merges two sequences and removes duplicates.
不过,我能以某种方式自定义重复删除行为 - 比方说,如果我想从第二个序列
重复的情况下,或者从第一个序列
。
But can I somehow customize the duplicate removal behavior - let's say if I wish to use the element from the second sequence
in case of duplicate or from the first sequence
.
或即使我希望得到的序列中以某种方式结合起来的价值观?
Or even if I wish to somehow combine those values in the resulting sequence?
的如何应该是被实施?的
我猜我错误地描述了问题,让我们说我们有一定的价值:
I guess I described the problem incorrectly, let's say we have some value:
class Value {
String name
Int whatever;
}
和使用的比较器执行 x.name = = y.name
支票。
and the comparer used performs a x.name == y.name
check.
和我们说,有时我知道我应该从第二顺序取元素,因为它的任何
字段更新/比第一序列的任何
字段更好。
And let's say that sometimes I know I should take the element from the second sequence, because it's whatever
field is newer / better than the whatever
field of the first sequence.
无论如何,我会使用 sequence1.Union(sequence2)
或 sequence2.Union(序列1)
的方法的变化。
Anyway, I would use the sequence1.Union(sequence2)
or sequence2.Union(sequence1)
variation of the methods.
感谢您
推荐答案
您可以使用 second.Union(第一)
而不是 first.Union(二)
。这样的话,它会不断从项目第二个
而不是从第一
。
You can use second.Union(first)
instead of first.Union(second)
. That way, it will keep the items from second
rather than the items from first
.
这篇关于C#LINQ工会问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!