来自可枚举结果的Concat字符串

时间:2018-12-14 15:32:54

标签: c# linq

如何实现以下目标?

string domains = String.Join(",",DomainsTable.AsEnumerable().Select(x => x.Field<string>("Domain").ToString()).ToArray());  
//D1,D2,D3,D4            

string orgs = String.Join(",", DomainsTable.AsEnumerable().Select(x => x.Field<string>("org").ToString()).ToArray());
//O1,O2,O3,O4

var DomainOrg = domains.Zip(orgs, (first, second) => first + "|" + second); 
// i was trying to zip for this but did not get expected result, how it can be done..we can go for other option also

Expected result = D1|O1,D2|O2,D3|O3,D4|O4  

上面的预期结果应该是字符串,因为我需要使用split函数来达到预期的结果

1 个答案:

答案 0 :(得分:0)

您不应该将两个源集合合并为一个字符串,因为Zip()将作为字符集合而不是字符串集合在字符串上进行操作。

以这个简化的示例为例:

var domains = new [] { "D1", "D2", "D3", "D4" };
var orgs = new [] { "O1", "O2", "O3", "O4" };

var domainOrgs = domains.Zip(orgs, (first, second) => first + "|" + second);

var joined = string.Join(",", domainOrgs);

现在joined包含:

  

D1 | O1,D2 | O2,D3 | O3,D4 | O4