Thierry Savard Saucier
Thierry Savard Saucier

Reputation: 449

Casting substrings with linq into a list of object and than sorting it base on property in vb.net

This have to be in vb.net linq, i'm pretty sure I could do it in c#, but I cant find any good enough translator to help me ... even the answers I find here in SO seems to only be written in linq, hence the question which might be a duplicate of a c# one.

That being said, considering these 2 classes :

Public class User
    Public Property Name() As String
    Public Property Teams As TeamList

      Public sub New(d as string, results as TeamList)
            me.name = d
            me.Teams = results
      end sub
end class

Public Class TeamList
    Public Property TeamName() As String
    Public Property fullscore() As list(of object) 

    Public sub New(name as string, value as list(of string))
        me.TeamName = name
        me.fullscore = value
        me.fullscore = getFullScore(value) (return a list of object)
    end sub
End Class 

I'm struggling in the final steps of my linq -to - object : (you can copy /paste this in linqpad)

Sub Main

 dim Definition as new Dictionary(of String, object) 

definition.add("user1_redTeam-02", new object)
definition.add("user1_redTeam-01", new object)
definition.add("user1_blueTeam-03", new object)
definition.add("user2_redTeam-01", new object)
definition.add("user1_redTeam-03", new object)
definition.add("user1_blueTeam-01",new object)
definition.add("user2_blueTeam-01", new object)
definition.add("user1_blueTeam-02", new object)
definition.add("user2_redTeam-02", new object)

Dim q3  = (From userlists In Definition.Keys.GroupBy(Function(s) s.Split("_")(0)) _
        Select New With _
        {.UserName = userlists.Key, _
         .animationList = (From scList In userlists.GroupBy(Of String)(Function(s) s.Split("-")(0)) _
        Select New With {.Team = scList.Key, _
                          .Score = scList.ToList()})})
  q3.dump()
End Sub

this is the result : Linq results

now, all I want is to sort the .score attribute (just a simple .sort(), and instead of returning an anonymous q3 object, which I,m cluless to transform, I'd like the q3 to be a list(of User)

it think it should looks like this ... but I cant make it works, i always gets some linq conversion errors : Unable to cast object of type 'WhereSelectEnumerableIterator2[System.Linq.IGrouping2[System.String,System.String],UserQuery+User]' to type 'System.Collections.Generic.List`1[UserQuery+User]'.

Dim q3 as List(of User)= (From userlists In Definition.Keys.GroupBy(Function(s) s.Split("_")(0)) _
Select New User(userlists.Key, (From scList In userlists.GroupBy(Of String)(Function(s) s.Split("-")(0)) _
Select New TeamList(scList.Key, scList.ToList()))))

Upvotes: 1

Views: 112

Answers (1)

Mark
Mark

Reputation: 8160

Your code examples seem to be incorrect - for example, it seems like User.Teams should be a list of some type, not a TeamList object, which isn't really a list. Anyway, with a little modification, this is what I came up with - maybe it's close to what you were looking for (a list of users with the scores sorted). You can paste into LINQPad to run it.

Sub Main
    Dim Definition As New Dictionary(of String, Object) 
    definition.add("user1_redTeam-02", New Object)
    definition.add("user1_redTeam-01", New Object)
    definition.add("user1_blueTeam-03", New Object)
    definition.add("user2_redTeam-01", New Object)
    definition.add("user1_redTeam-03", New Object)
    definition.add("user1_blueTeam-01",New Object)
    definition.add("user2_blueTeam-01", New Object)
    definition.add("user1_blueTeam-02", New Object)
    definition.add("user2_redTeam-02", New Object)
    Dim q3 = (
        From userlists In Definition.Keys.GroupBy(Function(s) s.Split("_"c)(0))
        Select New User(
            userlists.Key,
            (From scList In userlists.GroupBy(Function(s) s.Split("-"c)(0))
            Select New Team(scList.Key.Split("_"c)(1), scList.OrderBy(Function(s) s).ToList())).ToList()
        )
    ).ToList()
    q3.dump()
End Sub

' Define other methods and classes here
Public class User
    Public Property Name() As String
    Public Property Teams() As List(Of Team)
    Public Sub New(d As String, results As List(Of Team))
        Me.Name = d
        Me.Teams = results
    End Sub
End Class

Public Class Team
    Public Property TeamName() As String
    Public Property FullScore() As List(Of String) 
    Public Sub New(name As String, value As List(Of String))
        Me.TeamName = name
        Me.FullScore = value
    End Sub
End Class

Upvotes: 1

Related Questions