IEnumerable的多次枚举 - StackOverflowException

时间:2016-04-07 13:29:32

标签: c# linq deferred-execution

IEnumerable和Linq的一个有趣问题。

static void Main(string[] args)
{
    var subject = "#Accountancy #premier #Agile #Apache #automation #Automation #banking #Banking #bankIngs #AutoMation";
    var hashtags = subject.Split('#').Select(hashtag => hashtag.Trim().ToUpper()).Distinct();

    var plurals = hashtags.Where((hashtag) =>
    {
        return hashtags.Contains($"{hashtag.ToUpper()}S");
    }).Select(h => $"{h.ToUpper()}S");      //.ToList(); (1) - will not break

    //filter hashtags
    hashtags = hashtags.Except(plurals);    //.ToList(); (2) - will not break

    //if iterate, would break with:
    //System.StackOverflowException was unhandled Message: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
    foreach (var hashtag in hashtags)
    {
        Console.WriteLine(hashtag);
    }

    Console.Read();
}

很好奇如何解释为什么会发生溢出异常?

2 个答案:

答案 0 :(得分:4)

一步一步地完成它。

  1. 复数是 hashtags 中的每个单词,它也有相同的单词以s结尾
  2. Hashtags 是除复数以外的所有单词。
  3. 要执行2,您必须执行1.但是, hashtags 会不断变化,因此复数会尝试不在原始集合上执行,而是在2的结果上执行(同样,依赖于结果1)。

    您的查询将尝试:

    hashtags = hashtags.Except(plurals);
    

    替换plurals

    hashtags = hashtags.Except(
                hashtags.Where(hashtag => { return hashtags.Contains($"{hashtag.ToUpper()}S"); })
                        .Select(h => $"{h.ToUpper()}S")
               );
    

    hashtagshashtags.Except(plurals);

    hashtags.Except(
                hashtags.Except(plurals).Where(hashtag => { return hashtags.Contains($"{hashtag.ToUpper()}S"); })
                        .Select(h => $"{h.ToUpper()}S")
               );
    

    然后我们需要再次替换plurals等等。

    您的修补程序(添加.ToList())是修复它的合理方法。

答案 1 :(得分:3)

您正在将主题标签(尚未评估)重新分配给另一个导致无限循环的评估。如果将第二个评估放在另一个变量中,它将起作用:

var hashtags2 = hashtags.Except(plurals);

foreach (var hashtag in hashtags2)
{
    Console.WriteLine(hashtag);
}
相关问题