早上好,LINQ和IEnumerable的<String>重新评估
我写了下面的LINQ查询
public static Func<String, IEnumerable<String>> ListaCorreccoes = (StrPalavra) =>
{
return (from StrTmp in ListaPossiveisCorreccoes(StrPalavra)
from StrTmp2 in ListaPossiveisCorreccoes(StrTmp)
where PalavraConhecida(StrTmp2)
select StrTmp2).Distinct();
};
其中,出乎我的意料,比使用两个foreach
圈子中快得多。使用ListaTmp = ListaCorreccoes("comutador");
,其中ListaTmp
是IEnumerable<String>
类型的运行此功能后,我需要使用
Console.Write(">> Prima qualquer tecla para listar todas as " + ListaTmp.Count() + " correcções...");
打印ListaTmp
的基数和打印的ListaTmp
的内容,用
foreach (String StrTmp in ListaTmp)
Console.WriteLine(StrTmp);
然而,无论是最后和最后一行代码导致ListaTmp,因此查询为重新评估,这是非常奇怪的,因为变量被分配了查询结果。为什么这个代码有这么奇怪的行为?
非常感谢。