Avatar of jagguy
jagguy
Flag for Australia asked on

for each where

Is there a way in vb.net select all elements from a for each statement using a where?
I am not a fan of linq for speed purposes.
It is for a game type program so speed is critical.


I want to iterate through only some  elements of a list . Those elements meet some condition.
Like  iterate through a list of objects where some property is set to false.

I can use a for each and and if statement currently but I am after a better way.


  For Each pt In Mpl
            If pt.direction = missileType Then
...
Visual Basic.NET.NET ProgrammingC#

Avatar of undefined
Last Comment
Gururaj Badam

8/22/2022 - Mon
Wayne Taylor (webtubbs)

The only way to do that is with LINQ.    Dim pts = From pt In Mpl Where pt.Direction = missleType    For Each pt In pts        ''''    NextWayne
Mike Tomlinson

You can use the List.FindAll() method:
http://msdn.microsoft.com/en-us/library/fh1w7y8z.aspx

Really not sure if it is "better" though...just different...

Made up example:
Public Class Form1

    Private Const missileType As Integer = 0

    Private Class Something
        Public direction As Integer
    End Class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Mpl As New List(Of Something)

        ' ...

        For Each pt In Mpl.FindAll(AddressOf FindMissiles)
            ' ...
        Next
    End Sub

    Private Function FindMissiles(ByVal pt As Something) As Boolean
        Return pt.direction = missileType
        ' ... obviously a more complicted match algorithm could be used in here! ...
    End Function

End Class

Open in new window

HainKurt

what about creating a dynamic datatable, put elements into it (maybe you already getting values from db, in this case you may have a datatable already)
then you can use rowfilter property of datatable.defaultview.rowfilter="id > 10 and id < 20"

--> http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx
Your help has saved me hundreds of hours of internet surfing.
fblack61
jagguy

ASKER
thanks for the replies.

actually it looks worse with all these  extra functions and I am wooried about speed as well, database no way and MSDN examples dont always clarify things. Who wites MSDN stuff and does it get proof read for simplicity?
Gururaj Badam

I'm a C# guy, pardon my syntax.

For Each pt In Mpl.Where pt => pt.direction = missileType


this will only iterate elements matching your where condition. I don't know why you feel LINQ will slow down your processing when it's actually design gain performance.
jagguy

ASKER
i havent read anywhere Liunq speeds up performance and have only heard it slows things down because of its design.

Could you show me any article where it says iterating through a list of data using linq is quicker ( or no slwere)than using an if condition .
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Gururaj Badam

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.