none
How to Update Entries in a List here RRS feed

  • Question

  • Hi All, 

    I am getting bit confusion while updating list. Please help me.

    I have below 2 different lists 

    List<string> finalStudentList = new List<string> { "StudentNo1", "StudentNo2", "Student3", "StudentNo4", "Student5"};
    List<string> reportedStudentList = new List<string> { "StudentNo2", "StudentNo5" };
    

    i want to update the "finalStudentList", with matching entry in "reportedStudentList" with adding "Reported:" prefix, and other entieries with "NotReported" prefix.

    my output should be like below

    List<string> finalStudentList = new List<string> { "NotReported: StudentNo1", "Reported : StudentNo2", "NotReported: Student3", "NotReported: StudentNo4", "Reported : Student5" };

    Please help me to achieve this. 

    Thanks in Advance,

    Chandu


    Wednesday, July 11, 2018 10:53 AM

Answers

  • Try this:

    List<string> finalStudentList = new List<string> { "StudentNo1", "StudentNo2", "StudentNo3", "StudentNo4", "StudentNo5" };
    List<string> reportedStudentList = new List<string> { "StudentNo2", "StudentNo5" };
    
    var q = from fs in finalStudentList
            join rs in reportedStudentList on fs equals rs into ps
            select ( ps.Any() ? "Reported: " : "NotReported: " ) + fs;
    
    finalStudentList = q.ToList();
    



    Wednesday, July 11, 2018 11:37 AM