linq - takewhile() 是使用 yeild 之类的方法检查每次迭代,还是一次抓取一组元素?
问题描述
例如,假设我想做这样的事情:
bool foo(List<strings> stringList, int counter)//assume this list has, like, 10 elements, and counter=3, idk
{
bool found= false;
for(int i=0; i<stringlist.Count && !found; i++)
{
if(stringlist[i].length < 2 || counter >=6)
found=true;
counter++;
}
return found
}
现在,这是否等同于:
bool foo(List<strings> stringList, int counter)//assume this list has, like, 10 elements, and counter=3, idk
{
bool found= false;
foreach(string s in stringlist.Takewhile(x=> (!found)))
{
if(s.length < 2 || counter >=6)
found=true;
counter++;
}
return found
}
第二个示例的行为是否与第一个示例一样,还是总是跳过整个循环?作为后续,如果我仍然想使用 foreach,我真的必须使用 break 来解决这个问题吗?另外,对不起,如果我在这些示例中做了一些愚蠢的事情,我正在尝试简化我正在编写的寻路算法的一个版本,这是我能想到的最简单的示例来问这个问题......
解决方案
作为微软在这里的文档:
TakeWhile(IEnumerable, Func) 方法使用谓词测试 source 的每个元素,如果结果为真,则生成该元素。当谓词函数为元素返回 false 或源不包含更多元素时,枚举停止。
这是反编译的 TakeWhile() 方法:
public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null)
throw Error.ArgumentNull(nameof (source));
if (predicate == null)
throw Error.ArgumentNull(nameof (predicate));
return Enumerable.TakeWhileIterator<TSource>(source, predicate);
}
private static IEnumerable<TSource> TakeWhileIterator<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
foreach (TSource source1 in source)
{
if (predicate(source1))
yield return source1;
else
break;
}
}
如您所见,是的 TakeWhile() 方法循环遍历所有进行检查的元素,并且它的内部循环与您的外部循环无关。
推荐阅读
- javascript - 两个输入相同时启用按钮
- php - 如何使用按钮停止会话?
- c# - IIS System.IO.DirectoryNotFoundException
- avahi - avahi 编译错误“找不到 libgdbm.so”ubuntu 16.04?
- sql-server - 是否可以将 NULL 从 Typescript 传递到 SQL Server 存储过程
- c# - 此平台不支持文件加密
- azure - Azure:将 ASP.Net Core SDK 更新到 1.0 到 2.1.203 并收到有关 System.Data.SqlClient 的错误
- regex - Angular2-6、正则表达式和输入装饰器
- oauth-2.0 - Gitlab oauth2 访问令牌不能通过 http 使用 git
- java - 当您可以只使用具体类型时,为什么会有带有上限的通配符?