C#QueryExpression内部枚举数
我有一个电子邮件字符串数组,逗号分隔。例如:C#QueryExpression内部枚举数,c#,query-expressions,C#,Query Expressions,我有一个电子邮件字符串数组,逗号分隔。例如: string[] array = new [] { "test@test.com;test2@test.com", "stack@test.com;test@test.com" } 我想要一个查询表达式,它允许我枚举所有三个电子邮件地址(test@test.com这是一个副本:“test@test.com, test2@test.com, stack@test.com" 我的基本尝试如下 var enum = array.Select(
string[] array = new [] {
"test@test.com;test2@test.com",
"stack@test.com;test@test.com"
}
我想要一个查询表达式,它允许我枚举所有三个电子邮件地址(test@test.com这是一个副本:“test@test.com, test2@test.com, stack@test.com"
我的基本尝试如下
var enum = array.Select((item) => item.Split(';'));
结果是(当然)一个具有2个项的枚举数,每个项包含一个具有2个项的数组
我不知道该如何命名,所以我无法通过谷歌找到答案。如果您能给我一个提示或什么,我将不胜感激。您很可能正在寻找LINQ
SelectMany
方法:
var enum = array
.SelectMany(item => item.Split(';'))
.Distinct()
.ToArray();
这将返回一个带有唯一电子邮件的字符串数组。首先,您要使用
SelectMany
返回单个扁平列表:
array.SelectMany(item => item.Split(';'))
然后您要使用Distinct
来过滤重复项
.Distinct()
然后你就可以列举所有这些:
var emails = array.SelectMany(item => item.Split(';')).Distinct();
foreach (var email in emails) {
// do stuff here
}
另外,若要跳过重复项,请使用
Distinct()
方法。这正是我要查找的。非常感谢。