Sorting an Structured object - vb.net

Sorting:
Dim objReport(0) As stReport
Try
For intK = 1 To 15
objReport(intK).Name = "User" & GetRandomNumber()
objReport(intK).Age = intK
objReport(intK).Misc1 = "data"
Next
Return objReport
Is there any simple way to sort the objReport order by Name?
NOTE: The code is just a sample.

You can sort them using OrderBy Extension Function
objReport = objReport.OrderBy(Function(x) x.Name).ToArray()

Related

How can I use iteration to make this vbnet code more efficient?

Function set_bar_positions()
bar_x(0) = delocateX(bar1.Left)
bar_y(0) = delocateY(bar1.Top)
bar_x(1) = delocateX(bar2.Left)
bar_y(1) = delocateY(bar2.Top)
bar_x(2) = delocateX(bar3.Left)
bar_y(2) = delocateY(bar3.Top)
This snippet from one of my functions show what I'm trying to do. These lines repeat almost identical until the end of the function where this is called:
bar_x(29) = delocateX(bar30.Left)
bar_y(29) = delocateY(bar30.Top)
I have tried iterating this functions by doing stuff like this, but now I know I can't:
Dim num As Integer = 0
bar_x(num) = delocateX(bar(num)).Left)
I am trying to make this code more efficient and have less lines. Anyone have an idea I can implement?
You can use Controls.Find
For i = 0 To 29
Dim cs = Me.Controls.Find("bar" & i.ToString(), True)
If cs.Any() Then
Dim c = cs.First()
bar_x(i) = delocateX(c.Left)
bar_y(i) = delocateY(c.Top)
End If
Next

Substitution in LINQ OrderBy

I have tried the following to modify the sort order in order to have the fields with HeureDebut sorted to the end when it is 00:00 but it does not work
Is there another method to achieve this?
Dim theCompetitions As List(Of competitions) = (From aCompetition In context.competitions
Where aCompetition.concour_id = idConcours
And aCompetition.isPublic = 1
And aCompetition.idMYMaster = 0 _
Order By aCompetition.DateEpreuve, If(aCompetition.HeureDebut = "00:00", "99:99", aCompetition.HeureDebut), aCompetition.NoRef).ToList()

Multiple Dynamic Order By in Linq to Entity Framework

Dim receipts As IQueryable(Of ReceiptEntity) = db.Receipts
'code to filter removed for brevity
Dim sorts() As String = SortExpression.Split(";")
For Each sort As String In sorts
Dim sortParts() As String = sort.Split(" ")
If sortParts(1).ToLower = "true" Then
receipts = receipts.OrderBy(Of ReceiptEntity)(sortParts(0).ToString(), SortDirection.Ascending)
Else
receipts = receipts.OrderBy(Of ReceiptEntity)(sortParts(0).ToString(), SortDirection.Descending)
End If
Next
SortExpression comes in like "field1 true;field2 false;field3 true"
What I want to happen is for the query to have multiple order by fields, what is happening is that only the last order by is applied. What am I doing wrong here?
Here is what the working result looks like:
Dim receipts As IOrderedQueryable(Of ReceiptEntity) = db.Receipts.Include(Function(r) r.LineItems).Include(Function(r) r.Payments)
Dim sorts() As String = SortExpression.Split(";")
Dim sortParts() As String
sortParts = sorts(0).Split(" ")
If sortParts(1).ToLower = "true" Then
receipts = receipts.OrderBy(sortParts(0).ToString())
Else
receipts = receipts.OrderByDescending(sortParts(0).ToString())
End If
For Each sort As String In sorts.Skip(1)
sortParts = sort.Split(" ")
If sortParts(1).ToLower = "true" Then
receipts = receipts.ThenBy(sortParts(0).ToString())
Else
receipts = receipts.ThenByDescending(sortParts(0).ToString())
End If
Next
You have to use ThenBy instead of OrderBy for the second and all subsequent sort operations.

VB:NET using LINQ how can i find the row index for a specified string in some Datatable column

to check if a string exists in some column i use something like
mydatatable.AsEnumerable().Any(Function(r) r.Field(Of String)("somecolumn") = "somestring")
but how can i find the row index of "somestring"? considering its allowed to exist only once in mydatatable , and what if it existed more than once?
You can use the overload that passes the index:
Dim rows = myDataTable.AsEnumerable().
Select(Function(r, i) New With {.Row = r, .Index = i}).
Where(Function(x) x.Row.Field(Of String)("somecolumn") = "somestring")
If rows.Any() Then
Dim firstIndex As Int32 = rows.First.Index
End If

Join two items from an array using VB

I am creating an array as follows
Dim strFriends(0 to 6) As String
strFriends(0) = "Bianca"
strFriends(1) = "Jeana"
strFriends(2) = "Sam"
strFriends(3) = "Jenna"
strFriends(4) = "Erin"
strFriends(5) = "Carolyn"
strFriends(6) = "Kate"
Dim myFriends As String
myFriends = Join(strFriends, ", ")
MsgBox myFriends
This will produce the following string: "Bianca, Jeana, Sam, Jenna, Erin, Carolyn, Kate"
But I need to retrieve specific items in array and display them as list, something like this:
e.g: If I want to select from strFriends, Kate ,Sam and Bianca
It should list like
Kate
Sam
Bianca
How to perform the task. I am really new to VB so I am confused with this simple task. Can anyone help.
Thank you
Dim strFriends(0 To 6) As String
strFriends(0) = "Bianca"
strFriends(1) = "Jeana"
strFriends(2) = "Sam"
strFriends(3) = "Jenna"
strFriends(4) = "Erin"
strFriends(5) = "Carolyn"
strFriends(6) = "Kate"
Dim objOutput As Text.StringBuilder = New Text.StringBuilder()
For Each strFriend As String In strFriends
Select Case strFriend
Case "Kate", "Sam", "Bianca"
objOutput.AppendLine(strFriend)
End Select
Next
MessageBox.Show(objOutput.ToString())
Or
For Each strFriend As String In strFriends
If MyLogicToDetermineSelected(strFriend) Then
objOutput.AppendLine(strFriend)
End If
Next
It's not clear why you need to pick items from the original array when you already know which items you want, so I won't answer that part of the question. As for displaying each item on it's own line, you can do this with String.Join:
Dim chosenFriends As String() = {"Kate", "Sam", "Bianca"}
Dim output As String = String.Join(Environment.NewLine, chosenFriends)