locked
How to Update Entries in a List here RRS feed

  • Question

  • User321489315 posted

    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:51 AM

Answers

  • User-492460945 posted

    Hi chandu,

    Please check below working code,

    List<string> finalStudentList = new List<string> { "StudentNo1", "StudentNo2", "Student3", "StudentNo4", "StudentNo5" };
                List<string> reportedStudentList = new List<string> { "StudentNo2", "StudentNo5" };
    
                for (int i = 0; i < finalStudentList.Count; i++)
                {
                    int count = 0;
                    for (int j = 0; j < reportedStudentList.Count; j++)
                    {
                        if (finalStudentList[i] == reportedStudentList[j])
                            count++;
                    }
    
                    if (count > 0)
                        finalStudentList[i] = "reported: " + finalStudentList[i];
                    else
                        finalStudentList[i] = "notreported: " + finalStudentList[i];
                }
    

    Thanks,

    RajeshV.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2018 11:57 AM
  • User-369506445 posted

    Please try below code

    var union=finalStudentList.Union(reportedStudentList);
    
    var intersect=finalStudentList.Select(i=>i.ToString()).Intersect(reportedStudentList);
    
    List<string> final=List<string>();
    foreach(var item in union)
    {
    if(!intersect.Any(x=>x.Equals(item)))
    {
    final.Add("NotReported:"+item);
    }else
    {
    final.Add(item);
    }
    }

    In this sample ,first union two lists toghter and find intersect values between them and finally add additonal info to them

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2018 1:02 PM

All replies

  • User-492460945 posted

    Hi chandu,

    Please check below working code,

    List<string> finalStudentList = new List<string> { "StudentNo1", "StudentNo2", "Student3", "StudentNo4", "StudentNo5" };
                List<string> reportedStudentList = new List<string> { "StudentNo2", "StudentNo5" };
    
                for (int i = 0; i < finalStudentList.Count; i++)
                {
                    int count = 0;
                    for (int j = 0; j < reportedStudentList.Count; j++)
                    {
                        if (finalStudentList[i] == reportedStudentList[j])
                            count++;
                    }
    
                    if (count > 0)
                        finalStudentList[i] = "reported: " + finalStudentList[i];
                    else
                        finalStudentList[i] = "notreported: " + finalStudentList[i];
                }
    

    Thanks,

    RajeshV.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2018 11:57 AM
  • User-369506445 posted

    Please try below code

    var union=finalStudentList.Union(reportedStudentList);
    
    var intersect=finalStudentList.Select(i=>i.ToString()).Intersect(reportedStudentList);
    
    List<string> final=List<string>();
    foreach(var item in union)
    {
    if(!intersect.Any(x=>x.Equals(item)))
    {
    final.Add("NotReported:"+item);
    }else
    {
    final.Add(item);
    }
    }

    In this sample ,first union two lists toghter and find intersect values between them and finally add additonal info to them

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2018 1:02 PM