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

Hobbyist_programmer 621 Reputation points
2021-08-24T09:57:02.607+00:00

Hallo,

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

Thanks

VB
VB
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
    2021-08-24T10:29:23.117+00:00

    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
    2021-08-24T13:51:31.14+00:00

    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
    2021-08-25T05:43:03.943+00:00

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

    Module IEnumerableExtensions  
        <Extension()>  
        Public Sub ForEach(Of T)(ByVal source As IEnumerable(Of T), ByVal action As Action(Of T))  
            For Each element As T In source  
                action(element)  
            Next  
        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