Resharper:可能的多个枚举的IEnumerable(Resharper: Possible Multiple Enumeration of IEnumerable)

编程入门 行业动态 更新时间:2024-10-21 19:36:25
Resharper:可能的多个枚举的IEnumerable(Resharper: Possible Multiple Enumeration of IEnumerable)

我正在使用新的Resharper版本6.在我的代码中的几个地方,它强调了一些文本,并警告我可能会有一个可能的多次枚举的IEnumerable

我明白这是什么意思,并在适当的时候采取了建议,但在某些情况下,我不知道这实际上是一个大问题。

像下面的代码一样:

var properties = Context.ObjectStateManager.GetObjectStateEntry(this).GetModifiedProperties(); if (properties.Contains("Property1") || properties.Contains("Property2") || properties.Contains("Property3")) { ... }

它强调了每一次提到的properties在第二行,警告我列举了这个IEnumerable多次。

如果将.ToList()添加到行1的末尾(将properties从IEnumerable<string>转换为List<string> ),则警告消失。

但是肯定的是,如果我把它转换成一个List,那么它将枚举整个IEnumerable来构建List,然后根据需要枚举List,以查找属性(即1个完整的枚举和3个部分枚举)。 而在我的原始代码中,它只是做了3个部分枚举。

我错了吗? 这里最好的方法是什么?

I'm using the new Resharper version 6. In several places in my code it has underlined some text and warned me that there may be a Possible multiple enumeration of IEnumerable.

I understand what this means, and have taken the advice where appropriate, but in some cases I'm not sure it's actually a big deal.

Like in the following code:

var properties = Context.ObjectStateManager.GetObjectStateEntry(this).GetModifiedProperties(); if (properties.Contains("Property1") || properties.Contains("Property2") || properties.Contains("Property3")) { ... }

It's underlining each mention of properties on the second line, warning that I am enumerating over this IEnumerable multiple times.

If I add .ToList() to the end of line 1 (turning properties from a IEnumerable<string> to a List<string>), the warnings go away.

But surely, if I convert it to a List, then it will enumerate over the entire IEnumerable to build the List in the first place, and then enumerate over the List as required to find the properties (i.e. 1 full enumeration, and 3 partial enumerations). Whereas in my original code, it is only doing the 3 partial enumerations.

Am I wrong? What is the best method here?

最满意答案

我不知道你的properties真的在这里 - 但是如果它本质上代表一个非物质化的数据库查询,那么你的if语句将执行三个查询。

怀疑这样做会更好:

string[] propertiesToFind = { "Property1", "Property2", "Property3" }; if (properties.Any(x => propertiesToFind.Contains(x)) { ... }

这将在逻辑上仅迭代序列一次 - 如果涉及到数据库查询,则可能只需使用SQL“IN”子句在单个查询中的数据库中完成所有操作。

I don't know exactly what your properties really is here - but if it's essentially representing an unmaterialized database query, then your if statement will perform three queries.

I suspect it would be better to do:

string[] propertiesToFind = { "Property1", "Property2", "Property3" }; if (properties.Any(x => propertiesToFind.Contains(x)) { ... }

That will logically only iterate over the sequence once - and if there's a database query involved, it may well be able to just use a SQL "IN" clause to do it all in the database in a single query.

更多推荐

本文发布于:2023-08-04 11:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415294.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   Resharper   IEnumerable   Enumeration   Multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!