For Each is not a member if IEnumerable LINQ and .Net Framework 4.6

Hobbyist_programmer 621 Reputation points


I had a following line , it worked few days ago and then i updated my ClosedXML and it gives me this error "For Each is not a member if IEnumerable"

Anyidea to correct this error?

With MyList

 .ForEach.(Sub(c) If c.TSpec1 > 0 AndAlso Not c.UoM1.Equals("-") Then c.TSpec1 = Math.Round(UnitValue(CDbl(c.TSpec1), c.UoM1), 3, MidpointRounding.AwayFromZero)))

End With


An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,606 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 113K Reputation points

    Probably MyList is not a list. Check if ToList.ForEach works:

    With MyList
       .ToList.ForEach(Sub(c) . . . )
    End With

    It assumes that MyList consists of objects (not structures).

    But also clarify why MyList is not a List.

  2. Hobbyist_programmer 621 Reputation points

    sorry for the confusion, I meant to say "MyList" is not a real list .. it is a list with other names. The code worked previously

    0 comments No comments

  3. Xingyu Zhao-MSFT 5,356 Reputation points

    Hi @Hobbyist_programmer ,
    In order to avoid the exception, you can add the following extension method to your project.

    Module IEnumerableExtensions  
        Public Sub ForEach(Of T)(ByVal source As IEnumerable(Of T), ByVal action As Action(Of T))  
            For Each element As T In source  
        End Sub  
    End Module  

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments