Question code help using IEnumerable

Anu

New member
Joined
Jan 5, 2022
Messages
1
Programming Experience
Beginner
I need to fetch the details of the students who score more the 4 marks using IEnumerable interface in C#
C#:
using System;
using System.Linq;
using System.Collections.Generic;



public class Program
{
    public class Student {
        public string Name { get;set; }
        public decimal Avg { get; set; }
    }



    static public void Main()
    {
        var students = new [] {
            new Student {
                Name = "Andrzej",
                Avg = 5
                },
            new Student {
                Name = "Michał",
                Avg = 3.4m
                },
            new Student {
                Name = "Heniu",
                Avg = 2.4m
                },

            new Student {
                Name = "Bartek",
                Avg = 4.5m

                }

        };

        // TODO: get a list of students names with avg above 4 points
        IEnumerable<string> above4 = null;

        above4.Dump();
    }
}
 
Last edited by a moderator:
This is an inauspicious start to your time here. You posted unformatted code and even the original didn't contain any indenting, making it far more difficult for us to read. Worse, though, is that you have posted nothing else. You need to provide a FULL and CLEAR explanation of the problem, i.e. what you're trying to do, how you're trying to do it and what happens when you try. I'm guessing that the TODO in the code is an indication of the problem but that is hardly a comprehensive explanation. A note in the code is not nearly enough. Please try again and, this time, provide ALL the relevant information.
 
I need to fetch the details of the students who score more the 4 marks using IEnumerable interface in C#
C#:
using System;
using System.Linq;
using System.Collections.Generic;

public class Program {
  class Program {
    public class Student {
      public string Name { get; set; }
      public decimal Avg { get; set; }
    }
    static public void Main() {
      var students = new[] {
        new Student { Name = "Andrzej", Avg = 5 },
        new Student { Name = "Michał", Avg = 3.4m },
        new Student { Name = "Heniu", Avg = 2.4m },
        new Student { Name = "Bartek", Avg = 4.5m }};

      Console.WriteLine("Dot method chaining style");
      IEnumerable<string> above4a = students
        .Where(s => s.Avg >= 4m)
        .Select(s => s.Name);
      above4a.ToList().ForEach(name => Console.WriteLine(name));

      Console.WriteLine("\nLinq Query style");
      IEnumerable<string> above4b =
        from s in students
        where s.Avg >= 4m
        select s.Name;
      above4b.ToList().ForEach(name => Console.WriteLine(name));
    }
  }
}
Two examples to get there:
  • dot method chaining -- an approach more familiar for OOP developers.
  • Linq query syntax -- an approach more familiar for Functional Programming developers.

Ps. next time:
  • format your code
  • provide a summary of what you have already tried; code examples of your attempts are expected.
 
Last edited:
Back
Top Bottom