Link to home
Start Free TrialLog in
Avatar of indy500fan
indy500fan

asked on

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

Avatar of wdosanjos
wdosanjos
Flag of United States of America image

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

Avatar of Fernando Soto
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
Avatar of indy500fan
indy500fan

ASKER

Okay, I will try these out.
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?
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

Fernando, it's not allowing the "let" key word. It deletes let as soon as I type it and push space bar.
wdosanjos,


Thank you for your prompt replys...

 I still can't get his block to compile.

Any suggestion on the let key word?
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

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
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.


Sorry, and windows.
Can you post the error messages you are getting.
Also please post the code as you have implemented it.
   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

ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
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.
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
wdosanjos: are you okay with 50/50?