【发布时间】:2010-10-20 08:47:59
【问题描述】:
早上好,
我编写了以下 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 cicles 快得多。使用ListaTmp = ListaCorreccoes("comutador"); 运行此函数后,其中ListaTmp 的类型为IEnumerable<String>,我需要使用打印ListaTmp 的基数
Console.Write(">> Prima qualquer tecla para listar todas as " + ListaTmp.Count() + " correcções...");
并打印ListaTmp的内容,使用
foreach (String StrTmp in ListaTmp)
Console.WriteLine(StrTmp);
但是,代码的最后一行和倒数第二行都会导致 ListaTmp 并因此 重新评估查询,这非常奇怪,因为变量被分配了查询。为什么这段代码会有如此奇怪的行为?
非常感谢。
【问题讨论】: