Concat
是SQL中UNION ALL
的LINQ等效项。
我在LINQPad中设置了一个简单的例子来演示如何使用Union
和Concat
。如果你没有LINQPad,就明白吧。
为了能够看到这些设置操作的不同结果,第一组和第二组数据必须至少有一些重叠。在下面的例子中,两组都包含单词“不”。
打开LINQPad并将语言下拉菜单设置为C#语句。以下内容粘贴到查询窗格中并运行它:
string[] jedi = { "These", "are", "not" };
string[] mindtrick = { "not", "the", "droids..." };
// Union of jedi with mindtrick
var union =
(from word in jedi select word).Union
(from word in mindtrick select word);
// Print each word in union
union.Dump("Union");
// Result: (Note that "not" only appears once)
// These are not the droids...
// Concat of jedi with mindtrick (equivalent of UNION ALL)
var unionAll =
(from word in jedi select word).Concat
(from word in mindtrick select word);
// Print each word in unionAll
unionAll.Dump("Concat");
// Result: (Note that "not" appears twice; once from each dataset)
// These are not not the droids...
// Note that union is the equivalent of .Concat.Distinct
var concatDistinct =
(from word in jedi select word).Concat
(from word in mindtrick select word).Distinct();
// Print each word in concatDistinct
concatDistinct.Dump("Concat.Distinct");
// Result: (same as Union; "not" only appears once)
// These are not the droids...
在LinqPad结果看起来是这样的:
你应该庆祝乔恩·韦尔的答案为“接受” – arviman 2014-06-09 08:07:44