Note that there are some explanatory texts on larger screens.

plurals
  1. PODuplicate data and writing over data during a sort
    primarykey
    data
    text
    <p>I have the following code to go through a list of Student objects. The purpose is to iterate through each Student object in the list, one by one, access the Fees property (which is an array) of each student object, and sort the array of Fees for every student. In short, the purpose of the code is to sort each array of fees by DueDate, for each student object in the list. </p> <pre><code>foreach (var resultSet in globalResultSet) { for(var i=0; i &lt; resultSet.Length; i++) { resultSet[i].Fees = resultSet[i].Fees.ToList().OrderBy(x =&gt; x.DueDate).ToArray(); } } </code></pre> <p>The code works, but the problem is that because of the assignment operator, every unique student object will have the same set of sorted fees. </p> <p>So for example, after running the code, if student A has fees 1, 2, 3, then student B, C ... n will all also all have fees of 1, 2, 3 after the sort. </p> <p>What is the best way to fix this? </p> <p>I want to sort every student's fees by date, but I don't want them all to have the same results. All students should all have their own unique set of sorted fees, not all of them having the same.</p> <p>Please help. </p> <p><strong>-------- EDIT --------</strong></p> <p>As requested, here is an explanation of <code>globalResultSet</code>. </p> <p>What I'm doing is sending some student data via a wcf service to get student fees associated with each student. The code below shows how this process works. The important thing to note is that besides some basic data types, each student object has a property that is an array (This is where the fees for each student is stored). But when I try to sort the array for each individual student object, all the students get the same set of sorted fees. i.e,, they all have the same array of fees, when each student should instead have a unique array of sorted fees, after the sort. </p> <pre><code>// global variable private List&lt;MyService.Student[]&gt; globalResultSet = new List&lt;MyService.Student[]&gt;(); // then the rest in an event handler ServiceClient client = new ServiceClient(); MyService.Student student = new MyService.Student(); for (int i = 0; i &lt; students.Count; i++) { if (students[i].StudentID &gt; 0) { student.StudentID = students[i].StudentID; student.FirstName = students[i].FirstName; student.LastName = students[i].LastName; MyService.Student[] restultSet = client.GetFees(new MyService.Student[] { student }); globalResultSet.Add(restultSet); } else { continue; } } client.Close(); if (globalResultSet.Count &gt; 0) { MessageBox.Show("Data loaded", "", MessageBoxButtons.OK); } else { MessageBox.Show("Data NOT loaded", "Error", MessageBoxButtons.OK); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload