Possible multiple enumeration IEnumerable. How to decide? Do I need to decide?

I have the following code that gives a warning in the header. I am pretty sure that I did something similar before, but it did not give any warnings. I would like to ask two things from these posts. 1) What can cause the problem here? 2) Do I need to fix it?

The reason I'm asking is because this code is working fine, as I expect it to be so clear that this warning does not cause a problem. I can not resist warnings, etc. In my code, although I would like this solution, I also want to know why this warning occurs, and if it is harmful in any case.

the code:

 public class AttributeType
 {
      private string m_attributeNameField;

      public string AttributeName
      {
          get { return m_attributeNameField; }
          set { m_attributeNameField = value; }
      }
 }

 private StandardResponseType ValidateAttributes(string featureType, IEnumerable<AttributeType> attributeList, string userCategory)
 {
       StandardResponseType standardResponse = 
       new StandardResponseType(DateTime.Now.ToString(CultureInfo.InvariantCulture), "RWOL_UTILS.Get_Item_Attributes", "", "OK");

        if (attributeList.Any())
        {
            foreach (AttributeType attribute in attributeList)
            {
                if (attribute.AttributeName == null) continue;
                {
                    //do stuff
                }
            }
        }
        else
        {
            standardResponse.Message = "Error: No attributes passed in the list. ValidateAttributes().";
            standardResponse.ResponseCode = "FAIL";

            return standardResponse;
        }
}

EDIT: There is more code in this method, but it does not affect this problem.

: follwing, . ? , ? - . , , isnt. .

List<AttributeType> newlist = attributeList.ToList();

if (newlist.Count() != 0)
{
    foreach (AttributeType attribute in newlist)
............
+3
7

, IEnumerable. , ( , ), attributeList.Any().

Any(), , IEnumerable , ( , ).

: , . attributeList.ToArray() IEnumerable , , .

+3

if; .

Resharper, , attributeList , . ( Any() foreach)

+6

, attributeList.Any() attributeList -, for. foreach , .

.Any(), foreach , .

, foreach , , , .

+1

, attributeList - IEnumerable<>. IEnumerable , List s, , , . # yield return IEnumerable, , .

- , , . Any() foreach. , Any() , , ToList() ToArray() IEnumerable, /. - .

+1

. () :

        bool empty = true;
        foreach (AttributeType attribute in attributeList) 
        { 
            empty = false;
            if (attribute.AttributeName == null) continue; 
            { 
                //do stuff 
            } 
        } 
        if (empty)
        {
          {  
              standardResponse.Message = "Error: No attributes passed in the list.       ValidateAttributes().";  
              standardResponse.ResponseCode = "FAIL";  

              return standardResponse;  
          }  
        }

.Any() . = > "", , , "foreach".

attributeList , Count Length:

    if (attributeList.Count != 0)         
    {         
        foreach (AttributeType attribute in attributeList)         
        {         
            if (attribute.AttributeName == null) continue;         
            {         
                //do stuff         
            }         
        }         
    }         
    else         
    {         
        standardResponse.Message = "Error: No attributes passed in the list. ValidateAttributes().";         
        standardResponse.ResponseCode = "FAIL";         

        return standardResponse;         
    } 
0

, . , if (attributeList.Any()) else, , . , :

foreach (AttributeType attribute in attributeList.OfType<AttributeType>())
{
    // do stuff
}
0

, :

, .Any(), , .

This method is more readable, albeit rather ugly in the background (still effective).

        bool any;

        foreach (var i in Enumerable.Range(0, 100).Loop(out any))
        {
            // Do loop logic
        }

        if (!any)
        {
            // Handle empty IEnumerable
        }

How?!

public static class Ex
{
    public static IEnumerable<T> Loop<T>(this IEnumerable<T> source, out bool any)
    {
        var b = true;

        var enumerable = source.Loop(() => { b = false; });

        any = b;

        return enumerable;
    }

    private static IEnumerable<T> Loop<T>(this IEnumerable<T> source, Action anySetter)
    {
        var enumerator = source.GetEnumerator();

        enumerator.Reset();

        if (!enumerator.MoveNext())
        {
            anySetter();
            yield break;
        }

        do
        {
            yield return enumerator.Current;
        } while (enumerator.MoveNext());
    }
}
0
source

All Articles