Refer the below sample code for your reference and Try it in your code it generating same output positioning result as per subject by marks.
Refer the below code and impliment it as per your code logic.
C#
public class Records
{
public string Subject { get; set; }
public int Total { get; set; }
public int Position { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Subject",typeof(string))
,new DataColumn("Marks",typeof(string))
});
dt.Rows.Add("QWE", 99);
dt.Rows.Add("QWE", 88);
dt.Rows.Add("QWE", 88);
dt.Rows.Add("QWE", 66);
dt.Rows.Add("QWE", 99);
dt.Rows.Add("QWE", 66);
dt.Rows.Add("ASD", 99);
dt.Rows.Add("ASD", 66);
dt.Rows.Add("ASD", 55);
dt.Rows.Add("ASD", 55);
dt.Rows.Add("ASD", 99);
dt.Rows.Add("ASD", 55);
List<Records> records = new List<Records>();
for (int i = 0; i < dt.Rows.Count; i++)
{
records.Add(new Records
{
Subject = dt.Rows[i]["Subject"].ToString(),
Total = Convert.ToInt32(dt.Rows[i]["Marks"].ToString())
});
}
var TotaledRecords = (from p in records
select p).OrderByDescending(a => a.Subject).ThenByDescending(a => a.Total);
TotaledRecords.ToList()[0].Position = 1;
// Declare the Subject variable with default value.
string subject = TotaledRecords.ToList()[0].Subject;
// Declare the default value for subject Position value and repeated subject count
int currentPositionValue = 1;
int currentSubjectCount = 1;
// Declare bool variable to verify is new subject started.
bool newSubject = false;
for (int j = 1; j <= TotaledRecords.ToList().Count - 1; j++)
{
// Check if its new subject then only set below variable with new subject position values
if (subject != TotaledRecords.ToList()[j].Subject)
{
subject = TotaledRecords.ToList()[j].Subject;
currentPositionValue = 1;
currentSubjectCount = 0;
newSubject = true;
}
// check for the condition if marks not same and not the new subject
if ((!newSubject) && (TotaledRecords.ToList()[j].Total != TotaledRecords.ToList()[j - 1].Total))
{
TotaledRecords.ToList()[j].Position = currentPositionValue = currentSubjectCount + 1;
currentSubjectCount++;
}
else
{
TotaledRecords.ToList()[j].Position = currentPositionValue;
currentSubjectCount++;
newSubject = false;
}
}
}
}
VB.Net
Public Class Records
Public Property Subject As String
Public Property Total As Integer
Public Property Position As Integer
End Class
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable
dt.Columns.AddRange(New DataColumn() {New DataColumn("Subject", GetType(System.String)), New DataColumn("Marks", GetType(System.String))})
dt.Rows.Add("QWE", 99)
dt.Rows.Add("QWE", 88)
dt.Rows.Add("QWE", 88)
dt.Rows.Add("QWE", 66)
dt.Rows.Add("QWE", 99)
dt.Rows.Add("QWE", 66)
dt.Rows.Add("ASD", 99)
dt.Rows.Add("ASD", 66)
dt.Rows.Add("ASD", 55)
dt.Rows.Add("ASD", 55)
dt.Rows.Add("ASD", 99)
dt.Rows.Add("ASD", 55)
Dim records As List(Of Records) = New List(Of Records)
For i = 0 To dt.Rows.Count - 1
records.Add(New Records With {
.Subject = dt.Rows(i)("Subject").ToString(),
.Total = Convert.ToUInt32(dt.Rows(i)("Marks").ToString())})
Next
Dim TotaledRecords = From p In records
Select p
Order By p.Subject Descending, p.Total Descending
TotaledRecords.ToList(0).Position = 1
' Declare the Subject variable with default value.
Dim subject As String = TotaledRecords.ToList(0).Subject
' Declare the default value for subject Position value and repeated subject count
Dim currentPositionValue As Integer = 1
Dim currentSubjectCount As Integer = 1
' Declare bool variable to verify is new subject started.
Dim newSubject As Boolean = False
For j = 1 To TotaledRecords.ToList.Count - 1
' Check if its new subject then only set below variable with new subject position values
If (subject <> TotaledRecords.ToList(j).Subject) Then
subject = TotaledRecords.ToList(j).Subject
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)
Else
TotaledRecords.ToList(j).Position = currentPositionValue
currentSubjectCount = (currentSubjectCount + 1)
newSubject = False
End If
Next
Dim abc As String = ""
End If
End Sub