JPBrill
JPBrill

Reputation: 3

C# Lambda expressions with Classes

I am reading a csv with a list of students - Name, Surname, ClassLeader, Grade,Subject, Score.

I want to add a new student if he doesn't exist or only add the Subject and Score if the student exists in the list. Code below:

   class School
    {
        private int[] Grades = new int[5] { 8, 9, 10, 11, 12 };
        public List<Student> Students = new List<Student>();
        private HashSet<string> AllSubjects = new HashSet<string>();

        public School()
        {

        }

        public void CreateStudents()
        {                
            List<string[]> storedCSVData = CSVHelper.ReadCSV();
            //int index = 0;

            foreach(string[] lineItem in storedCSVData)
            {
                //index++;
                //if ((index % 6) != 0)
                //    continue;
                string fullName = lineItem[0] + " " + lineItem[1];

                int i = Students.IndexOf(x =>  
                x.GetFullName().Contains(fullName));

                    if(i >= 0){
                    Students[i].SubjectScore.Add(lineItem[4], 

                    Convert.ToDouble(lineItem[5]));
                        continue;
                    }                         

                    Student storedStudent = new Student(lineItem[0],
                                                        lineItem[1],
                                                        lineItem[2] == "Yes" 
                                                        `? true : false,`

                   Convert.ToInt32(lineItem[3]));

                    Students.Add(storedStudent);

            }

            foreach(Student s in Students)
            Console.WriteLine(s.GetFullName());

        }
    }

}

In student class:

    class Student : Person
    {
        private bool ClassLeader = false;
        private int Grade = 0;
        public Dictionary<string, double> SubjectScore = new 
                                          Dictionary<string, double>();


        public Student(string name, string surname, bool classLeader, int 
                       grade)
        {
            Name = name;
            Surname = surname;
            ClassLeader = classLeader;
            Grade = grade;
        }

        public string GetFullName()
        {
            return Name + " " + Surname;
        }


    }

I keep getting an error that says Cannot convert lambda expression of type 'Student' because it is not a delegate type.

Can someone please help with this, I'm currently lost.

Upvotes: 0

Views: 2121

Answers (2)

mwilczynski
mwilczynski

Reputation: 3082

Since you're using reference to Student, searching it in list and then retrieving its index doesn't make sense. You are using Student instance anyway, so use LINQ's FirstOrDefault and retrieve object you are trying to modify (in this case, to change the SubjectScore).

You'd rather:

foreach(string[] lineItem in storedCSVData)
{

    string fullName = lineItem[0] + " " + lineItem[1];

    //Get student instance instead of index, since you would use it anyway
    Student student = Students.FirstOrDefault(s => s.GetFullName().Contains(fullName));

    //If there is no result, FirstOrDefault returns 'null'
    if(student != null)
    {
        //Add using refernce instead of using index
        student.SubjectScore.Add(
            lineItem[4], 
            Convert.ToDouble(lineItem[5]));
        continue;
    }                         

    Student storedStudent = new Student(lineItem[0],
                                        lineItem[1],
                                        lineItem[2] == "Yes" ? true : false,
                                        Convert.ToInt32(lineItem[3]));

    Students.Add(storedStudent);

}

Upvotes: 2

SelvaS
SelvaS

Reputation: 2125

It seems you are passing the Lambda expression for an IndexOf method which only supports an Item instead of Lambda. Try to get the Student using Lambda and use the Student to get the index.

Student student = Students.FirstOrDefault(x => x.GetFullName().Contains(fullName));
int i = Students.IndexOf(student);

Upvotes: 1

Related Questions