Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

for each where

Posted on 2010-09-03
7
Medium Priority
?
345 Views
Last Modified: 2012-05-10
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
...
0
Comment
Question by:jagguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 33601628
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33601700
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

0
 
LVL 59

Expert Comment

by:HainKurt
ID: 33601754
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
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

by:jagguy
ID: 33601903
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?
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33602163
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.
0
 

Author Comment

by:jagguy
ID: 33605978
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 .
0
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 2000 total points
ID: 33606127
@jagguy - I couldn't believe until i did a test. You're right.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question