?
Solved

Need a LINQ Qry to get data from a Hashtable in VB.net

Posted on 2011-05-06
21
Medium Priority
?
1,767 Views
Last Modified: 2013-11-11
Friends,

I need a LINQ qry in VB.NET to get data from a HashTable:

The SQL Version would be:

 select Count(ElapsedTime) as Laps Where LapSpeed > 190 and LapSpeed < 200
Where DriverID = 100

Looking for LINQ syntax in vb.net.

Thanks in advance!
'Here is the Syntax for My hashtable:

Public Shared LapTimes_HT As New Hashtable

'Here is the syntax for my class clsLapDetail:

Public Class clsLapDetail
    Public Lap As Integer
    Public LapTime As Integer
    Public PassingTime As Integer
    Public ElapsedTime As Integer
    Public LapSpeed As Decimal
End Class

'here is how I add to the hashtable:

                Dim LapDetail As New clsLapDetail
                    LapDetail.ElapsedTime = ElapsedTime
                    LapDetail.Lap = LapCount
                    LapDetail.PassingTime = PassingTime
                    LapDetail.LapTime = Laptime
                    LapDetail.LapSpeed = LapSpeed


                    Dim LT_KEY As String = RunID & "-" & DriverID & "-" & LapCount

                    SyncLock LapTimes_HT

                        If Not LapTimes_HT.ContainsKey(LT_KEY) Then
                            LapTimes_HT.Add(LT_KEY, LapDetail)
                        End If
                    End SyncLock

Open in new window

0
Comment
Question by:indy500fan
  • 11
  • 6
  • 4
21 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35707703
Please try the following:

Dim cnt As Integer = LapTimes_HT.Values.Cast(Of clsLapDetail).Count(Function(l) l.LapSpeed > 190 and l.LapSpeed < 200)

Open in new window

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 35707855
Hi indy500fan;

A query like this should do what you need, if not let me know.

Dim query = (From laps in LapTimes_HT.Keys
             Let key = laps.Split("-")(1)
             Let Value = LapTimes_HT(laps)
             Where Value.LapSpeed > 190 And Value.LapSpeed < 200 And key = DriverID
             Select Value.ElapsedTime).Sum(Function(et) et)

Open in new window


Fernando
0
 

Author Comment

by:indy500fan
ID: 35708723
Okay, I will try these out.
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 

Author Comment

by:indy500fan
ID: 35708857
wdosanjos, your example doesn't address DriverID, I could add DriverID to my clsLapDetail if it would make it easier; however, driverID can be derived from the Key.

FernandoSoto,

Hey!  It's been a long time, how are you?

Hey, it doesn't like your example.

I'm getting a bunch of "is not declared"

laps, Value, Where, DriverID and some other stuff.

It's acting like it doesn't recognize is as Linq.

I am importing SYSTEM.LINQ, but it still doesn't like it.  Any other suggestions?
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35708954
If you can change your Hashtable to a Dictionary as follows, @FernandoSoto's code should work:

Public Shared LapTimes_HT As New Dictionary(Of String, clsLapDetail)() 'Hashtable

Open in new window

0
 

Author Comment

by:indy500fan
ID: 35708958
Fernando, it's not allowing the "let" key word. It deletes let as soon as I type it and push space bar.
0
 

Author Comment

by:indy500fan
ID: 35709029
wdosanjos,


Thank you for your prompt replys...

 I still can't get his block to compile.

Any suggestion on the let key word?
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35709053
Alternatively you can try the following with Hashtable:

	Dim cnt As Integer = (From entry In LapTimes_HT
				 Let key = CType(entry.key, String)
				 Let value = CType(entry.value, clsLapDetail)
				 Where key.Contains("-100-") And value.LapSpeed > 190 And value.LapSpeed < 200).Count()

Open in new window

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 35709101
Hi indy500fan;

I am doing just fine and hope you are doing the same.

I just tested my code and it worked fine on my system. What version of Visual Studio are you using and the .Net Framework? Windows or Web?

Can you post the code as you implemented it?

Thanks;
Fernando
0
 

Author Comment

by:indy500fan
ID: 35709147
Guys, neither of your solutions will compile.

I am using VS2008 (targeting .net framework 3.5)

I have Import System.Linq

and I have references to System.Data.DataSetExtensions and System.Data.Linq

But it won't recognize linq.


0
 

Author Comment

by:indy500fan
ID: 35709153
Sorry, and windows.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 35709181
Can you post the error messages you are getting.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 35709189
Also please post the code as you have implemented it.
0
 

Author Comment

by:indy500fan
ID: 35709208
   Private Sub PopulateROPListview()
    Dim cnt As Integer = (From entry In LapTimes_HT
        key = CType(entry.key, String)
        value = CType(entry.value, clsLapDetail)
                         Where key.Contains("-100-") And value.LapSpeed > 190 And value.LapSpeed < 200).Count()
    End Sub



Error	1	')' expected.	C:\EJK\My Projects\Base\VS2008\ROPTracker\ROPTracker\frmROPMain.vb	368	51	ROPTracker
Error	2	Name 'key' is not declared.	C:\EJK\My Projects\Base\VS2008\ROPTracker\ROPTracker\frmROPMain.vb	369	9	ROPTracker
Error	3	Name 'entry' is not declared.	C:\EJK\My Projects\Base\VS2008\ROPTracker\ROPTracker\frmROPMain.vb	369	21	ROPTracker
Error	4	Name 'value' is not declared.	C:\EJK\My Projects\Base\VS2008\ROPTracker\ROPTracker\frmROPMain.vb	370	9	ROPTracker
Error	5	Name 'entry' is not declared.	C:\EJK\My Projects\Base\VS2008\ROPTracker\ROPTracker\frmROPMain.vb	370	23	ROPTracker
Error	6	Name 'Where' is not declared.	C:\EJK\My Projects\Base\VS2008\ROPTracker\ROPTracker\frmROPMain.vb	371	6	ROPTracker
Error	7	Method arguments must be enclosed in parentheses.	C:\EJK\My Projects\Base\VS2008\ROPTracker\ROPTracker\frmROPMain.vb	371	12	ROPTracker
Error	8	Name 'key' is not declared.	C:\EJK\My Projects\Base\VS2008\ROPTracker\ROPTracker\frmROPMain.vb	371	12	ROPTracker
Error	9	Name 'value' is not declared.	C:\EJK\My Projects\Base\VS2008\ROPTracker\ROPTracker\frmROPMain.vb	371	38	ROPTracker
Error	10	Name 'value' is not declared.	C:\EJK\My Projects\Base\VS2008\ROPTracker\ROPTracker\frmROPMain.vb	371	63	ROPTracker
Error	11	End of statement expected.	C:\EJK\My Projects\Base\VS2008\ROPTracker\ROPTracker\frmROPMain.vb	371	83	ROPTracker

Open in new window

0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 1000 total points
ID: 35709264
Try adding continuation chars on the lines as follows:

	Dim cnt As Integer = (from entry in LapTimes_HT _
						 let key = CType(entry.key, String) _
						 let value = CType(entry.value, clsLapDetail) _
						 Where key.Contains("-100-") And value.LapSpeed > 190 And value.LapSpeed < 200).Count()

Open in new window

0
 

Author Comment

by:indy500fan
ID: 35709313
Duh...

Man, I've been programming in CCL the last 7 months and I've forgotten basic stuff.  Sorry.  Now, let me test your guys's solutions.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 35709387
Not having the continuation characters in the code and also not having the Let keywords caused your errors. In VS 2010 you do not need to have the continuation characters any more they are optional.

Fernando
0
 

Author Comment

by:indy500fan
ID: 35710574
Okay guys...

Fernando, yours is not doing exactly what I would have thought. It is giving me some crazy Numbers.  It's like it is adding the values of elapsed time, rather than counting the number of times it sees rows that match the criteria.

wdosanjos is actually working as I asked; however both solutions are really stressing out my processor.  In my sample situation, it is looking through over 17,000 records in the hashtable/dictionary as it is doing this counting.  This is a bit extreme, but possible.

I am going to ask how to possibly  make this more efficient in another question.
0
 

Author Comment

by:indy500fan
ID: 35710579
Now, for the dreaded part.  How do I fairly divide up the points?

I never want to tick either party off.  You guys were great, but wdosanjos did give me the solution I am going to use.  Fernando, i don't want to tick you off.  You have been an awesome source of knowledge for me.
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 total points
ID: 35710743
Hi indy500fan;

Just change my Sum method to Count method as shown in the code snippet below.

Dim query = (From laps In LapTimes_HT.Keys
             Let key = laps.Split("-")(1)
             Let Value = LapTimes_HT(laps)
             Where Value.LapSpeed > 190 And Value.LapSpeed < 200 And key = DriverID
             Select Value.ElapsedTime).Count()

Open in new window


Point split 50/50 sounds OK by me.

Fernando
0
 

Author Comment

by:indy500fan
ID: 35711857
wdosanjos: are you okay with 50/50?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 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