locked
LIST Having Array of MArks LINQ RRS feed

  • Question

  • User1203305613 posted

    Hi Folks ,

    I am trying to learn LINQ , when trying to work on an example to create says average marks, I am getting compilation error .

    How do I code array of marks in List.   

    using System;

    using System.Linq;
    using System.Collections.Generic;


    public class Program
    {
    public static void Main()
    {
    List<Student> Students = new List<Student>()

    {
    new Student(101, "Hugo", "Garcia", new List<int>() { 91, 88, 76, 93 }),
    new Student(102, "Rick", "Adams", new List<int>() { 70, 73, 66, 90 }),
    new Student(103, "Michael", "Tucker", new List<int>() { 73, 80, 75, 88 }),
    new Student(104, "Fadi", "Fakhouri", new List<int>() { 82, 75, 66, 84 }),
    new Student(105, "Peter", "Barrows", new List<int>() { 67, 78, 70, 82 })
    };

    var query = from student in Students
    where student.Marks.AsQueryable().All(m => m > 70)
    select student;
    foreach (Student student in query)
    {
    Console.WriteLine(student);
    }

    }
    }


    public class Student{
    public int StudentID ;
    public string FirstName ;
    public string LastName;
    public int[] Marks = new int[4] ;

    }

    Friday, June 2, 2017 5:48 PM

Answers

  • User1577371250 posted

    Hi,

    try this sample

    protected void Page_Load(object sender, EventArgs e)
            {
                List<Student> Students = new List<Student>()
                { 
                    new Student() { StudentID = 101, FirstName = "Hugo",    LastName = "Garcia",    Marks = new List<int> { 91, 88, 76, 93 }},
                    new Student() { StudentID = 102, FirstName = "Rick",    LastName = "Adams",     Marks = new List<int> { 70, 73, 66, 90 }},
                    new Student() { StudentID = 103, FirstName = "Michael", LastName = "Tucker",    Marks = new List<int>() { 73, 80, 75, 88 }},
                    new Student() { StudentID = 104, FirstName = "Fadi",    LastName = "Fakhouri",  Marks = new List<int>() { 82, 75, 66, 84 }},
                    new Student() { StudentID = 105, FirstName = "Peter",   LastName = "Barrows",   Marks = new List<int>() { 67, 78, 70, 82 }}
                };
    
                var query = (from student in Students
                             where student.Marks.Average() > 70
                             select new
                             {
                                 StudentId = student.StudentID,
                                 FirstName = student.FirstName,
                                 LastName = student.LastName,
                                 Marks = student.Marks,
                                 Avg = student.Marks.Average()
                             }).ToList();
            }
        }
    
        public class Student
        {
            public int StudentID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public List<int> Marks { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 2, 2017 6:02 PM

All replies

  • User1068175894 posted

    Im not sure if your question is about the student marks array declaration:

    convert:

    public int[] Marks = new int[4] ;

    to

    public List<int> Marks = new List<int>() ;

    Or the missing student constructor:

    public class Student{
    public int StudentID ;
    public string FirstName ;
    public string LastName;
    public int[] Marks = new int[4] ;
      public Student(int stdId, string fname, string lname, int[] mks ){
         StudentID = stdId;
         FirstName = fname;
         LastName = lname;
         Marks = mks;
      }
    
    }

    or about keeping the array declaration in the constructor and convert the list parameter to array like in:

    new Student(101, "Hugo", "Garcia", (new List<int>() { 91, 88, 76, 93 }).ToArray()  ),

    or about the query that could be:

    var query = Students.Where( s=>s.Marks.All(m=>m>70) );

    Friday, June 2, 2017 6:02 PM
  • User1577371250 posted

    Hi,

    try this sample

    protected void Page_Load(object sender, EventArgs e)
            {
                List<Student> Students = new List<Student>()
                { 
                    new Student() { StudentID = 101, FirstName = "Hugo",    LastName = "Garcia",    Marks = new List<int> { 91, 88, 76, 93 }},
                    new Student() { StudentID = 102, FirstName = "Rick",    LastName = "Adams",     Marks = new List<int> { 70, 73, 66, 90 }},
                    new Student() { StudentID = 103, FirstName = "Michael", LastName = "Tucker",    Marks = new List<int>() { 73, 80, 75, 88 }},
                    new Student() { StudentID = 104, FirstName = "Fadi",    LastName = "Fakhouri",  Marks = new List<int>() { 82, 75, 66, 84 }},
                    new Student() { StudentID = 105, FirstName = "Peter",   LastName = "Barrows",   Marks = new List<int>() { 67, 78, 70, 82 }}
                };
    
                var query = (from student in Students
                             where student.Marks.Average() > 70
                             select new
                             {
                                 StudentId = student.StudentID,
                                 FirstName = student.FirstName,
                                 LastName = student.LastName,
                                 Marks = student.Marks,
                                 Avg = student.Marks.Average()
                             }).ToList();
            }
        }
    
        public class Student
        {
            public int StudentID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public List<int> Marks { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 2, 2017 6:02 PM
  • User1203305613 posted

    Thanks Lokesh. Your solution worked. Just for my understanding I modified my Console Application

    using System;
    using System.Linq;
    using System.Collections.Generic;


    public class Program
    {
    public static void Main()
    {
    List<Student> Students = new List<Student>()

    {
    new Student() { StudentID = 101, FirstName = "Hugo", LastName = "Garcia", Marks = new List<int> { 91, 88, 76, 93 }},
    new Student() { StudentID = 102, FirstName = "Rick", LastName = "Adams", Marks = new List<int> { 70, 73, 66, 90 }},
    new Student() { StudentID = 103, FirstName = "Michael", LastName = "Tucker", Marks = new List<int>() { 73, 80, 75, 88 }},
    new Student() { StudentID = 104, FirstName = "Fadi", LastName = "Fakhouri", Marks = new List<int>() { 82, 75, 66, 84 }},
    new Student() { StudentID = 105, FirstName = "Peter", LastName = "Barrows", Marks = new List<int>() { 67, 78, 70, 82 }}
    };

    var query = (from student in Students
    where student.Marks.Average() > 70
    select new
    {
    StudentId = student.StudentID,
    FirstName = student.FirstName,
    LastName = student.LastName,
    Marks = student.Marks,
    Avg = student.Marks.Average()
    }).ToList();


    foreach (var x in query)
    {
    Console.WriteLine(x.LastName + x.FirstName + x.Avg);


    }


    }
    }

    public class Student
    {
    public int StudentID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<int> Marks { get; set; }
    }

    Friday, June 2, 2017 6:51 PM
  • User1203305613 posted

    Thanks Betancourt,

    Will try your suggestions , thanks for your advise on this. 

    Friday, June 2, 2017 6:52 PM