dcsimg
Results 1 to 4 of 4

Thread: partition ranking with ties vb.net 2010

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2017
    Posts
    25

    partition ranking with ties vb.net 2010

    Code:
    Dim TotaledRecords = From p In db.Assessments
                                 Where p.Term = cboterm.Text And p.Year = cboyear.Text
                                 Select p
                                 Order By p.Subject, p.Total Descending
    
    
            TotaledRecords.ToList(0).Position = 1
            For j = 1 To TotaledRecords.Count - 1
                If TotaledRecords.ToList(j).Mark <> TotaledRecords.ToList(j - 1).Mark Then
                    TotaledRecords.ToList(j).Rank = j + 1
                Else
                    TotaledRecords.ToList(j).Rank = TotaledRecords.ToList(j - 1).Rank
    
                End If
    
    
            Next
            db.SubmitChanges()


    Hello

    with the codes above this is what i get


    Code:
    Subject   Mark	Rank
    QWE        9       1
    QWE	     88	 3
    QWE	     99	 1
    QWE	     88	 3
    QWE	     66	 5
    QWE	     66	 5
    ASD	     99	 5
    ASD	     66	 6
    ASD	     55	 4
    ASD	     99	 5
    ASD	     55	 7
    ASD	     55	 7

    can someone help me modify the codes to get the result displayed in the table below
    or a better option

    Code:
    Subject  Mark	Rank
    QWE	     99	 1
    QWE	     88	 3
    QWE	     99	 1
    QWE	     88	 3
    QWE	     66	 5
    QWE	     66	 5
    ASD	     99	 1
    ASD	     66	 3
    ASD	     55	 4
    ASD	     99	 1
    ASD	     55	 4
    ASD	     55	 4
    i want the Rank to Re-start the number from 1 when the Subject change
    thank you
    Last edited by si_the_geek; Dec 16th, 2017 at 04:02 PM. Reason: added Code tags

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,205

    Re: partition ranking with ties vb.net 2010

    Thread moved from the 'CodeBank VB.Net' forum (which is for you to post working code examples, not questions) to the 'VB.Net' forum


    When you post code please put it inside code tags so it is displayed in a more readable way - either using the Code/VBCode buttons in the post editor screen (or at the top of the Quick Reply box), or by putting them in manually, like this: [code] code here [/code]

    I have added them to your post (including for the example data, to help the formatting there).


    As to the question... I have no idea what you want, as your example data (and minimal explanation) doesn't make it clear what end result you want, especially as your example data doesn't seem to have any ordering.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2017
    Posts
    25

    Re: partition ranking with ties vb.net 2010

    Code:
    Order By  p.Stream Descending, p.ElSubject Descending, p.Total Descending
            ' Declare the Subject variable with default value.        
            Dim subject As String = TotaledRecords.ToList(0).ElSubject
            Dim stream As Integer = TotaledRecords.ToList(0).Stream
            ' Declare the default value for subject Position value and repeated subject count  
            Dim currentPositionValue As Integer = 1
            Dim currentSubjectCount As Integer = 1
            Dim currentStreamCount As Integer = 1
            ' Declare bool variable to verify is new subject started.  
            Dim newSubject As Boolean = False
            Dim newstream As Boolean = False
    
            For j = 1 To TotaledRecords.ToList.Count - 1
    
                
                If (stream <> TotaledRecords.ToList(j).Stream) Then
                    stream = TotaledRecords.ToList(j).Stream
                    currentPositionValue = 1
                    currentSubjectCount = 0
                    currentStreamCount = 0
                    newstream = True
                End If
    
    
                ' Check if its new subject then only set below variable with new subject position values  
                If (subject <> TotaledRecords.ToList(j).ElSubject) Then
                    subject = TotaledRecords.ToList(j).ElSubject
                    currentPositionValue = 1
                    currentSubjectCount = 0
                    newSubject = True
                End If
                ' check for the condition if marks not same and not the new subject       
                If (Not newSubject AndAlso (TotaledRecords.ToList(j).Total <> TotaledRecords.ToList((j - 1)).Total)) Then
    
                    currentPositionValue = (currentSubjectCount + 1)
                    TotaledRecords.ToList(j).Position = (currentSubjectCount + 1)
                    currentSubjectCount = (currentSubjectCount + 1)
                    currentStreamCount = (currentStreamCount + 1)
                Else
                    TotaledRecords.ToList(j).Position = currentPositionValue
                    currentSubjectCount = (currentSubjectCount + 1)
                    newSubject = False
                End If

    This code meet my requirement
    Thanks your
    Last edited by Shaggy Hiker; Dec 16th, 2017 at 06:09 PM. Reason: Fixed the tag.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,503

    Re: partition ranking with ties vb.net 2010

    I edited those two posts to fix up the tag. Si didn't mean that code/vbcode were the tags, those are the buttons. The Code button is the #, while the VB Code button is the VB button. You can just press the button and paste the code between the resulting tags.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width