1

Is it possible to replace the foreach statement with a Linq command?

            string recipient = "recipients could not be defined"; 
            if (mail.To != null && mail.To.Count > 0) {
                var addresses = mail.To.Select(r => r.Address);
                foreach (string s in addresses) {
                    recipient += s + " ";
                }
            }

If I had ReSharper then I'm sure it would have come up with an appropriate suggestion but I don't. This question uses Concat and that feels like it should help but I'm not certain as to what object to use in the Concat.

4

1 に答える 1

9

Linq foreach (独自に作成した場合を除き、でのみ使用可能) を使用するのではなくList<T>、逆方向に実行して、プロセスでの文字列連結を回避できます。

recipient = string.Join(" ", mail.To.Select(r => r.Address));

var addresses...これはあなたの以降を置き換えます。

于 2013-09-30T12:09:56.473 に答える