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

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

indy500fanAsked:
Who is Participating?
 
wdosanjosConnect With a Mentor Commented:
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
 
wdosanjosCommented:
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
 
Fernando SotoRetiredCommented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
indy500fanAuthor Commented:
Okay, I will try these out.
0
 
indy500fanAuthor Commented:
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
 
wdosanjosCommented:
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
 
indy500fanAuthor Commented:
Fernando, it's not allowing the "let" key word. It deletes let as soon as I type it and push space bar.
0
 
indy500fanAuthor Commented:
wdosanjos,


Thank you for your prompt replys...

 I still can't get his block to compile.

Any suggestion on the let key word?
0
 
wdosanjosCommented:
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
 
Fernando SotoRetiredCommented:
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
 
indy500fanAuthor Commented:
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
 
indy500fanAuthor Commented:
Sorry, and windows.
0
 
Fernando SotoRetiredCommented:
Can you post the error messages you are getting.
0
 
Fernando SotoRetiredCommented:
Also please post the code as you have implemented it.
0
 
indy500fanAuthor Commented:
   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
 
indy500fanAuthor Commented:
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
 
Fernando SotoRetiredCommented:
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
 
indy500fanAuthor Commented:
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
 
indy500fanAuthor Commented:
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
 
Fernando SotoConnect With a Mentor RetiredCommented:
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
 
indy500fanAuthor Commented:
wdosanjos: are you okay with 50/50?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.