Using VB.NET, Framework 4.0, EF5, VS2010
As a beginner in Linq I have tried a lot of things, but can't make it to work.
Dim ctx As New Context
Dim query = (From a In ctx.TblAddress
Where (a.ID = 103)
Order By a.StreetAddress
Select a.ID, a.StreetAddress).ToList()
This entity I then bind to a RadGrid.
But I want to sort the StreetAddresses as alphanumeric.
I can managed it to work with an array of strings like this:
Dim address As String() = {"Street Name C21", "Street Name A12",
"Street Name B26", "Street Name B17", "Street Name C2", "Street Name B6"}
Dim sortedAddresses() As String = address.OrderBy(Function(s) Regex.Match(s, "^\D").Length =0).ThenBy(Function(s) Regex.Match(s, "\D*").Value).ThenBy(Function(s) Int32.Parse(Regex.Match(s,"\d+").Value)).ToArray()
but I can't make it to work in the query.
Or even in a second query. Something like this:
FAILS:
Dim q2 = query.AsEnumerable().OrderBy(Function(s) Regex.Match(s, "^\D").Length = 0).ThenBy(Function(s) Regex.Match(s, "\D*").Value)
I also saw a MSSQL query that did the work, but I rather get it to work in linq to entities.
The SQL query looked like this (and it did the job):
Select StreetAddress, Row_Number() Over (Order by Case When IsNumeric(ID) = 1 then Right(Replicate('0',21) + ID, 20)
When IsNumeric(ID) = 0 then Left(ID + Replicate('',21), 20)
Else ID
END) As RowNumber, ID From TblAddress where ID = 103
And yes, I have done a lot of googling for 2 days now, and searched here at SO.. With no luck so far :-(
I also saw a way to the solution here Alphanumeric sorting using LINQ but I couldn't figure out how to solve it.
Then I saw this solution in C# but for a ListBox, and now were getting closer to the solution (at least that's what I thought...) But after trying to make it to work with a RadGrid I finally gave it up..
var list = listBox.Items.Cast<ListItem>()
.OrderBy(item => int.Parse(item.Text.TrimStart('B')));
listBox.Items.Clear();
listBox.Items.AddRange(list.ToArray());
I'm totally lost, and take pieces everywhere without expected result. :)
Please assist...