Converting LINQ to dictionary in vb.net - vb.net

I'm trying to convert LINQ to dictionary in vb.net, but can't seem to understand why I get all these exceptions thrown at me.
All I'm trying to do, is to sort by descending value with help from LINQ.
Here's my code:
'Declaring my primary dictionary
Dim nameValDict As New Dictionary(Of Object, Object)
... nameValDict is now getting filled up with data
'Declaring temporary sorted dictionary with LINQ
Dim sortedDict = (From entry In nameValDict Order By entry.Value Descending Select entry)
'Replace with sorted results from LINQ
nameValDict = sortedDict.ToDictionary(Function(x) x.Key, Function(x) x.Value)
Exception: System.ArgumentException (Object should be of type Double) - On last line.
I'll appreciate ANY help.

Its properly your Order By in this statement:
From entry In nameValDict Order By entry.Value Descending Select entry
Becuase entry.Value is of type Object, and you can't sort desending on Object. The program doesnt know how to compare Objects against eachother.

Related

Using Linq order by an object's property Vb.Net

I have a datatable of pointshape Objects in a map and I want to order this list by Object.baseshape.label.y which is a double value. How can I do this using Linq?
I Have implemented this code so far
Dim query As IEnumerable(Of DataRow) = From result In dataArray.AsEnumerable() Order By result.Field(Of Object)("MapShapes") Descending
but I want something like this
Dim query As IEnumerable(Of DataRow) = From result In dataArray.AsEnumerable() Order By result.Field(Of Object)("MapShapes")..baseshape.label.y Descending
You can use the specific object type in the Field(Of ...) like this...
Dim query = From result In dataArray.AsEnumerable() Order By result.Field(Of MapSuite.BaseMapShape)("MapShapes").label.y Descending

How can I obtain a directory's file list sorted by timestamp?

I am stuck on this sorting problem.
Private Sub ...
Dim oDirInfo As DirectoryInfo
Dim aoFSInfo() As FileSystemInfo
Dim asFiles() As String
FQPN is a fully qualified path name ending in "\*.*".
oDirInfo = New DirectoryInfo(FQPN)
Now into asFiles I want the files' names, sorted by the files' timestamps in ascending order. I presume, that oDirInfo.CreationTime plays a role here, but can not figure out how to use OrderBy properly.
aoFSInfo = oDirInfo.GetFileSystemInfos() '?
asFiles = aoFSInfo.OrderBy(...)
End Sub
Yes, that's LINQ and you can use this (method-)syntax:
asFiles = oFSInfo.
OrderBy(Function(fsi) fsi.CreationTime).
Select(Function(fsi) fsi.FullName).
ToArray()
If you don't like the ugly Function keyword you can use query syntax:
Dim orderedFiles = From fsi In oFSInfo
Order by fsi.iCreationTime Ascending
Select fsi.FullName
asFiles = orderedFiles.ToArray()
Even if these are two statements it's not slower than method syntax due to deferred execution.

How to return a List(Of String) from a LINQ statement with a Group By in VB.net?

I've seen several questions on how to do this in C# but I'm having trouble translating those to VB. Here's the basics of my issue:
Table of data NOT normalized and accessed via Entity Framework
Get all unique string values in a certain field
Convert those values to a List(Of String)
This works but I'm guessing there's a better way to do it without iterating through the list:
Public Function GetGroups() As IEnumerable(Of String)
Dim GroupList As New List(Of String)
Dim CodeList = (From c In Context.Codes
Group c.Group By c.Group Into g = Group)
For Each c In CodeList
GroupList.Add(c.Group)
Next
Return GroupList
End Function
What I seem to be struggling with the most is using Group By in LINQ. I'm guessing this could probably be done in 1 or 2 lines by having LINQ return just the list of strings or by converting the list of anonymous objects to a list of strings.
Well, if you don't need anything in the group, you can just use .Distinct():
Return (
From c In Context.Codes
Order By c.Group
Select c.Group
).Distinct().ToList()
Edit: Added Order By

Search DataTable with values from another table

I'm trying to search one DataTable with values from anotherDataTable using LINQ, but no progress so far... How to do it?
In example below i have table, in which i search, and PlTable, which has only one column; and i need to retrieve every row from table, in which the Name field contains at least one string of Name field in PlTable's rows.
Dim ePlTable As IEnumerable(Of DataRow) = PlTable.AsEnumerable()
Dim found = From row In table.AsEnumerable
Where row(0).Contains(ePlTable)
Select row
Return found.CopyToDataTable.Rows
Surely it does not work, as .Contains wants String as argument
Surely it does not work, as .Contains wants String as argument
That's exatly the problem, so use the strongly typed Field extension
method to cast it to it's correct type and Enumerable.Any to look if at least one string is contained in this Name:
Dim strings = From row In PlTable Select row.Field(Of String)(0)
Dim found = From row In table.AsEnumerable
Where strings.Any(Function(s) row.Field(Of String)("Name").Contains(s))
Select row
Return found.CopyToDataTable()

Sorting numbers in descending order

I have 20 textboxes. each contains a particular number . I want the textbox1 to textboxN to have the numbers in the descending order. If any of the textbox has a zero value then I want to leave that textbox as it is. A sample code in vb.net needed.
'for sorting the elements in descending order
dim array(4) as integer
array(4)={4,6,2,9,1}
'first sort the array and then reverse it as
array.sort(4)
array.reverse(4)
sortlistbox.item.add(array(4))
Dim txt As New List(Of TextBox)
Dim q = From i In txt
Where CInt(i.Attributes("value")) > 0
Order By CInt(i.Attributes("value")) Descending
Select i
Whana try some simple linq query over your collection?
This one is a bit old, but I ran into the same problem.
Using MSDN I found this: Enumerable.OrderBy Method (IEnumerable, Func)
If you just add .Reverse to that query, it's descending:
Dim query As IEnumerable(Of Pet) = pets.OrderBy(Function(pet) pet.Age).Reverse
#Thom Morgan
This one is a bit old, but I ran into the same problem.
Using MSDN I found this: Enumerable.OrderBy Method (IEnumerable, Func)
If you just add .Reverse to that query, it's descending:
Dim query As IEnumerable(Of Pet) = pets.OrderBy(Function(pet) pet.Age).Reverse
This worked like a charm! Thank you!