nirh1989
nirh1989

Reputation: 209

Print array with LINQ

I got the following code and i am trying to print teenAgerStudents without the foreach i made after the LINQ line. Can i add print in the LINQ line? Can i print with another new LINQ line onstead the foreach?

class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public int Age { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        Student[] studentArray = {
                new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
                new Student() { StudentID = 2, StudentName = "Steve",  Age = 21 } ,
                new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
                new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
                new Student() { StudentID = 5, StudentName = "Ron" , Age = 31 } ,
                new Student() { StudentID = 6, StudentName = "Chris",  Age = 17 } ,
                new Student() { StudentID = 7, StudentName = "Rob",Age = 19  } ,
    };
        Student[] teenAgerStudents = studentArray.Where(s => s.Age > 12 && s.Age < 20).ToArray();

        foreach (var item in teenAgerStudents)
        {
            Console.WriteLine(item.StudentName);
        }
};

Upvotes: 7

Views: 16948

Answers (4)

Alexander Derck
Alexander Derck

Reputation: 14498

This will work:

studentArray.Where(s => s.Age > 12 && s.Age < 20)
            .ToList()
            .ForEach(s => Console.WriteLine(item.StudentName));

Array.ForEach takes an Action<T> so it doesn't have a return value. If you need the array later on, you should stick to your old code.

Of course you can also still use the ForEach in a second statement:

List<Student> teenAgerStudent = studentArray.Where(s => s.Age > 12 && s.Age < 20).ToList();
teenAgerStudent.ForEach(s => Console.WriteLine(s.StudentName));

But that makes it less readable (in my opinion) so I would stick to a good old foreach loop in that case.

Upvotes: 15

m.phobos
m.phobos

Reputation: 403

This line will print your teenAgerStudents names to console, one for each line.

Console.Write(string.Join(Environment.NewLine, studentArray.Where(s => s.Age > 12 && s.Age < 20).Select(s=>s.StudentName)));

It replaces both the teenAgerStudents initialization and the foreach.

If you still want to initialize your teenAgeStudents list because you need it, you could split the previous line in two

//get the teenager students in form of IEnumerable<Student>.
//cast it to array with .ToArray() after .Where() if needed.
var teenAgerStudents = studentArray.Where(s => s.Age > 12 && s.Age < 20);
//print only the students' names
Console.Write(string.Join(Environment.NewLine,teenAgerStudents.Select(s=>s.StudentName)));

Hope this helps.

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186813

Try this:

  // There's no need in materialization, i.e. "ToArray()"
  var target = studentArray 
    .Where(student => student.Age > 12 && student.Age < 20) // teen
    .Select(student => String.Format("Id: {0} Name: {1} Age {2}", 
              student.Id, student.Name, student.Age));

  // Printing out in one line (thanks to String.Join)
  Console.Write(String.Join(Environment.NewLine, target));

Upvotes: 3

Ren&#233; Vogt
Ren&#233; Vogt

Reputation: 43916

I don't like to use LINQ (a functional programming system) for side effects. I'd rather change the output foreach to something like that:

Console.WriteLine(string.Join(Environment.NewLine, 
    teenAgerStudents.Select(s => s.StudentName));

Upvotes: 2

Related Questions