none
C# - Find a solution using LINQ or something else RRS feed

  • Question

  • I have some code, like the following:

    class Program
    {
      public static List<CourseCredit> Old_Selection = new List<CourseCredit>();
      public static List<CourseCredit> New_Selection = new List<CourseCredit>();
      public static List<CourseCredit> Withdrawn = new List<CourseCredit>();
    
      public struct CourseCredit
      {
          public int CourseID { get; set; }
          public int Credits { get; set; }
    
      }
      static void Main(string[] args)
      {
          Old_Selection.Add(new CourseCredit { CourseID = 1, Credits = 100 });
          Old_Selection.Add(new CourseCredit { CourseID = 2, Credits = 200 });
          Old_Selection.Add(new CourseCredit { CourseID = 3, Credits = 300 });
          Old_Selection.Add(new CourseCredit { CourseID = 4, Credits = 400 });
          Old_Selection.Add(new CourseCredit { CourseID = 5, Credits = 500 });
          Old_Selection.Add(new CourseCredit { CourseID = 6, Credits = 600 });
          Old_Selection.Add(new CourseCredit { CourseID = 7, Credits = 700 });
          Old_Selection.Add(new CourseCredit { CourseID = 8, Credits = 800 });
          Old_Selection.Add(new CourseCredit { CourseID = 9, Credits = 900 });
          Old_Selection.Add(new CourseCredit { CourseID = 10, Credits = 1000 });
          Old_Selection.Add(new CourseCredit { CourseID = 11, Credits = 1100 });
    
          Withdrawn.Add(new CourseCredit { CourseID = 4, Credits = 400 });
          Withdrawn.Add(new CourseCredit { CourseID = 9, Credits = 900 });
    
          New_Selection.Add(new CourseCredit { CourseID = 1, Credits = 100 });
          New_Selection.Add(new CourseCredit { CourseID = 2, Credits = 200 });
          New_Selection.Add(new CourseCredit { CourseID = 3, Credits = 300 });
          New_Selection.Add(new CourseCredit { CourseID = 4, Credits = 500 });
          New_Selection.Add(new CourseCredit { CourseID = 5, Credits = 600 });
          New_Selection.Add(new CourseCredit { CourseID = 6, Credits = 700 });
          New_Selection.Add(new CourseCredit { CourseID = 7, Credits = 800 });
          New_Selection.Add(new CourseCredit { CourseID = 8, Credits = 1000 });
          New_Selection.Add(new CourseCredit { CourseID = 9, Credits = 1100 });
     }
    }

    The situation is like this:

    There are a number of courses with CourseID and Credits (the credit is most important).  There are the old selections for the courses, here it is used as in Old_Selection; and for some reason, there is Withdrawn of courses; so the old selection will not work, have to generate a new selection.  The rule is: for the courses, whose CourseID is less than the first Withdrawn CourseID, then keep them; for the courses, whose CourseID is at least as the Withdrawn CourseID, then replace the CourseID with the Credits, which is corresponding to the next CourseID’s credit.

    For example:

    For the courses: {CourseID = 1, 2, 3}, then keep them as in Old_Selection.

    For the courses: {CourseID = 4}, as it is in Withdrawn, then replace it with CourseID = 4, but with the Credits = 500, which is from {CourseID = 5, Credits = 500} in the Old_Selection.

    I think a good way to find the New_Selection is to delete the Courses in Withdrawn and then “shift” the Credits from the Old_Selection.

    But I can’t figure out how to implement it in C# yet, I prefer doing this using LINQ, but any idea is welcome.


    Tuesday, July 17, 2018 2:35 PM

All replies

  • Is there an inconsistency in your example? 

    You shifted the Credits of all CourseCredit objects, beginning with those CourseID >= 4, to the Credits value of the CourseCredit object with CourseID+1. But you raised the Credits of the CourseCredit object with CourseID 8 not to 900, as expected, but to 1000?

    wizend
    Wednesday, July 18, 2018 8:42 AM
  • Hi,

    This is a little confusing, but my point here is: the key is NOT the CourseID, but the key is the Credits.  So, the CourseID can be changed, but the Credits are not changeable.

    Sorry for the confusion.

    Wednesday, July 18, 2018 10:09 AM