Pradeep Lowanshi
Pradeep Lowanshi

Reputation: 19

I have problems with the looping in list

Making Lists of the student who have completed their graduation in 4 years and another list of student who took more or less than 4 years to complete their graduation and print the both the lists by taking the entries form user.

I have made the list the code I used is also working but I used two foreach loop for printing the two lists but I want to use only one loop to reduce the length of my code. Below is the code I used as a beginner.

public void Print(List<Student> students)
{
    foreach (Student student in students)
    {
        if (student.Time == "4")
        {
            Console.WriteLine($"\nPeople who completed  graduation in 4 years : \n{student.Name}");
        }
    }

    foreach (Student student in students)
    {
        if (student.Time != "4")
        {
            Console.WriteLine($"\nPeople who completed graduation in more or less than 4 years : \n{student.Name}\t{student.Time}");
        }
    }
}

Upvotes: 0

Views: 63

Answers (2)

tgralex
tgralex

Reputation: 814

I think you are looking for something like this:

public void Print(List<Student> students)
{
    var studentsIn4Years = new List<string>();
    var studentsNotIn4Years = new List<string>();
    foreach (var student in students)
    {

        if (student.Time == "4")
        {
            studentsIn4Years.Add(student.Name);
        }
        else
        {
            studentsNotIn4Years.Add($"{student.Name}\t{student.Time}");
        }
    }

    Console.WriteLine($"\nPeople who completed  graduation in 4 years: ", string.Join(", ", studentsIn4Years));
    Console.WriteLine($"\nPeople who completed graduation in more or less than 4 years: ", string.Join(", ", studentsNotIn4Years));
}

However, if the number of student is not significant, I'd recommend thinking about simplicity and not pseudo-optimization and achieve that with LINQ like this:

public void Print2(List<Student> students)
        {
            var studentsIn4Years = students.Where(s => s.Time == 4).Select(s => s.Name);
            var studentsNotIn4Years = students.Where(s => s.Time == 4).Select(s => $"{student.Name}\t{student.Time}");

            Console.WriteLine($"\nPeople who completed  graduation in 4 years: ", string.Join(", ", studentsIn4Years));
            Console.WriteLine($"\nPeople who completed graduation in more or less than 4 years: ", string.Join(", ", studentsNotIn4Years));
        }

Upvotes: 0

andyb952
andyb952

Reputation: 2008

To combine the statements and only use one foreach loop just use an else.

public void Print(List students) 
{
    foreach (Student student in students)
    {
        if (student.Time =="4")
        {
            Console.WriteLine($"\nPeople who completed  graduation in 4 years : \n{student.Name}");
        }
        else
        {
            Console.WriteLine($"\nPeople who completed graduation in more or less than 4 years : \n{student.Name}\t{student.Time}");
        }
    }
}

If you are after two list variables so that you can do extra logic use linq and .Where()

public void Print(List students) 
{
    var studentsIn4Years = students.Where(s => s.Time == "4");
    var studentsNotIn4Years = students.Where(s => s.Time != "4");

    // Do your logic here.
}

Upvotes: 2

Related Questions