Linq Collection 在 foreach 的下一次迭代中重置
我有以下 foreach
表达式,在其中构建谓词,然后通过执行 .Where()
过滤集合。
但令我困惑的是,即使在下一次迭代中执行 .Where()
之前, result.Count()
也会给我 0
。
var result = SourceCollection;
foreach (var fieldName in FilterKeys)
{
if (!conditions.ContainsKey(fieldName)) continue;
if (!conditions[fieldName].IsNotNullOrEmpty()) continue;
var param = conditions[fieldName];
Func<BaseEntity, bool> predicate = (d) => fieldName != null && d.GetFieldValue(fieldName).ContainsIgnoreCase(param);
result = result.Where(predicate);
}
有人知道我可能忽略的任何 LINQ 行为导致了这种情况吗?
I have the following foreach
expression within which I am building a predicate and then filtering the collection by executing .Where()
.
But what stumps me is, result.Count()
gives me 0
even before I execute .Where()
in the next iteration.
var result = SourceCollection;
foreach (var fieldName in FilterKeys)
{
if (!conditions.ContainsKey(fieldName)) continue;
if (!conditions[fieldName].IsNotNullOrEmpty()) continue;
var param = conditions[fieldName];
Func<BaseEntity, bool> predicate = (d) => fieldName != null && d.GetFieldValue(fieldName).ContainsIgnoreCase(param);
result = result.Where(predicate);
}
Does, anybody know of any LINQ behavior that I might have overlooked that is causing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想,你想要这样:
注意谓词中使用
f
。您不想捕获 foreach 变量。在原始代码中,当第二次迭代开始时, param 仍然是第一次迭代中捕获的值,但 fieldName 已更改。I think, you want this:
Notice the use of
f
in the predicate. You don't want to capture the foreach variable. In your original code, when the second iteration starts, param is still the captured value from the first iteration, but fieldName has changed.我同意这个问题来自于没有捕获 foreach 变量,但还有一个更深层次的问题,那就是 LINQ 与命令式控制流结构的组合 - 即混合 LINQ 和 foreach 变量。首先是
foreach
。请尝试这样做:
不需要
foreach
循环,并且代码纯粹保留在 LINQ 中。朋友不应该让朋友混合命令式和功能性......:-)I agree that the issue comes from not capturing the
foreach
variable, but there is a deeper issue and that is the combination of LINQ with imperative control flow structures - i.e. mixing LINQ &foreach
in the first place.Try this instead:
No
foreach
loop needed and the code stays purely in LINQ. Friends shouldn't let friends mix imperative and functional... :-)