Sorting numbers in descending order - vb.net

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!

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

DataTable.Select.Where VB.Net - Delete rows

I'm currently pulling information using a query that I'm not allowed to tamper with:
Dim dt As DataTable = BLL.GetData(variable).Tables(0)
Immediately afterwards, I'm removing any records where a field begins with a specific value:
For Each dr As DataRow In dt.Rows
If dr.Item(2).ToString().StartsWith("value") Then
dr.Delete()
End If
Next
What I'd really like to do is something like:
dt.Select.Where(field1 => field1.StartsWith("value")).Delete()
I know that is not the syntax of it and I'm probably very off from what it would be like. The For Each works fine, I'm just trying to "simplify" it. Any idea? Any and all help is appreciated.
Actually, your initial code is probably the cleanest and most straight forward.
To delete items using LINQ, you first need to read them into a separate collection, then loop through that collection and call Delete on each record. If you'd rather go that route, you could try:
Dim records = dt.Rows.Where(Function(r) r.StartsWith("value")).ToList()
For Each r In records
r.Delete()
Next
The answer I think you are looking for is below from Microsoft. https://msdn.microsoft.com/en-us/library/det4aw50(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
Dim table As DataTable = DataSet1.Tables("Orders")
' Presuming the DataTable has a column named Date.
Dim expression As String
expression = "Date > #1/1/00#"
Dim foundRows() As DataRow
' Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression)
Dim i As Integer
' Print column 0 of each returned row.
For i = 0 to foundRows.GetUpperBound(0)
Console.WriteLine(foundRows(i)(0))
Next i

Linq join typeddatatable with List and return typeddatatable

I hava a strongly typed datatable and a list(of String).
I want to build a linq query to return a datatable of the same type where the fields of a certain column of the table are in the list. I thought of doing a Join, although in normal sql I would have added
SELECT FROM Table WHERE Table.ID IN(...);
This is what I tried in linq.
Dim Families As List(Of String)
Dim Articles As SomeStronglyTypedDataTable
Dim MatchingArticles = From a In Articles.AsEnumerable _
Join f In Families.AsEnumerable On a.FamilyCode Equals f.ToString _
Select New With {}
I'm not sure either if I need to convert the query result back to a datatable nor if that's even possible.
Thanks!
Try the simpler query:
Dim MatchingArticles = From a In Articles.AsEnumerable _
Where Families.Contains(a.FamilyCode)_
Select a
Dim MyMatchingArticlesTable = CopyToDataTable(Of SomeStronglyTypedDataTable) (MatchingArticles)
Yes, you can do this. Instead of Select New ..., select the matching DataRows, Select a, and then use CopyToDataTable(Of T) on the matching rows.
Dim table As DataTable = query.CopyToDataTable()
Dim typedtable As New TypedDataset.TypedDataTable
typedtable.Merge(table)
I was raking my brain trying to get something similar to work, and your code enlightened me.
All I needed was to add the .AsEnumerable() on both sides.
I'm working with C#. Anyway, I think all you need to do is select your table like
Dim MatchingArticles = From a In Articles.AsEnumerable _
Join f In Families.AsEnumerable On a.FamilyCode Equals f.ToString _
Select a;
Well, this is a very old post, but hey, it might help someone else...
If you think this would resolve your question, please mark it as correct, so others will know. You may also want to mark Devart's answer as correct. I tried it and it works.