简体   繁体   English

Linq ForEach的简单传统foreach

[英]Simply traditional foreach to Linq ForEach

I have a list of strings like: 我有一个像这样的字符串列表:

abcd@domain.com
efgh@domain.com
ijkl@domain.com;mnop@domain.com;qrst@domain.com
uvwx@domain.com
yz@domain.com

I would like to want it as: 我想要它为:

abcd@domain.com
efgh@domain.com
ijkl@domain.com
mnop@domain.com
qrst@domain.com
uvwx@domain.com
yz@domain.com

So I wrote the code below and it works as expected. 所以我在下面编写了代码,它可以按预期工作。

foreach (var email in emailAddressesOnRequest)
{
    if (!string.IsNullOrEmpty(email) && email.Contains(';'))
    {
        emailAddressesOnRequest.AddRange(email.Split(';').ToList());
        emailAddressesOnRequest.Remove(email);
    }
}

Is there any way to simply it to LINQ ForEach? 有什么方法可以将它简单地传递给LINQ ForEach吗?

What you are looking for is to iterate through the collection and for each item to return an item of a different kind. 您正在寻找的是遍历该集合,并为每个项目返回一个不同种类的项目。 For that use Select . 为此,请Select

Because in your case you possibly want to return from each item a collection of items, and don't want to have them in nested collections use SelectMany on the result of the Split(';') method. 因为在您的情况下,您可能希望从每个项目返回一个项目集合,并且不想将它们放入嵌套集合中,所以请对Split(';')方法的结果使用SelectMany

List<string> values = new List<string>
{
    "abcd@domain.com",
    "efgh@domain.com",
    null,
    "ijkl@domain.com; mnop @domain.com; qrst @domain.com",
    "uvwx@domain.com",
    "yz@domain.com"
};

var result = values.Where(value => !string.IsNullOrWhiteSpace(value))
                   .SelectMany(value => value.Split(';')).ToList();

And in query syntax: 并在查询语法中:

var result = (from value in values
             where !string.IsNullOrWhiteSpace(value)
             from email in value.Split(';')
             select email).ToList();
var query = from line in emailAddressesOnRequest
            where !String.IsNullOrEmpty(line)
            from email in line.Split(';')
            select email;

What helped me a lot to understand ling was The standard LINQ operators 标准的LINQ运算符对我的理解很有帮助

If you split each string into substrings by semicolon, you get a collection of string sequences, or an IEnumerable<IEnumerable<string>> 如果用分号将每个字符串分成子字符串,则会得到字符串序列的集合,或者是IEnumerable<IEnumerable<string>>

The IEnumareable extension function to convert them to an IEnumerable<string> is Enumerable.SelectMany . 将它们转换为IEnumerable<string>IEnumareable扩展函数是Enumerable.SelectMany When iterating over a SelectMany it is like you do a nested foreach: 当遍历一个SelectMany ,就像您做了一个嵌套的foreach:

List<string[]> listOfStringArrays = ...
List<string> outputList = new List<string>();
foreach (string[] stringArray in listOfStringArrays)
{
     foreach (string str in stringArray)
     {
         outputList.Add(str);
     }
}

In your example the inner foreach is done using AddRange . 在您的示例中,内部的foreach是使用AddRange完成的。

Using Select and Split you convert your collection of strings to a sequence of string sequences. 使用SelectSplit您可以将字符串集合转换为字符串序列序列。 SelectMany will make it a sequence of strings: SelectMany将使它成为字符串序列:

IEnumerable<string> myInputStrings = ...
IEnumerable<string> outputStrings = inputStrings
    .Select(inputString => inputString.Split(';'))
    .SelectMany(splitResult => splitResult);

The Select will take each of the inputStrings, and split them by semicolon. Select将采用每个inputString,并用分号将它们分开。 The output is a string array, which implements IEnumerable<string> , even if your input didn't have a semicolon. 输出是一个字符串数组,即使您的输入没有分号,它也实现IEnumerable<string>

The SelectMany concatenates every string sequence of you sequence of string sequences. SelectMany连接字符串序列序列中的每个字符串序列。 The result is one sequence of strings. 结果是一个字符串序列。

To convert to array or list use ToArray() or ToList() . 要转换为数组或列表,请使用ToArray()ToList()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM