C# Lambda expressions with Classes

Multi tool use
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.
My apologies. Under class School the method CreateStudents() the line: int i = Students.IndexOf(x => x.GetFullName().Contains(fullName));
– Jp Brill
Jul 1 at 14:09
2 Answers
2
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
).
Student
index
Student
FirstOrDefault
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);
}
Thanks a lot. This has solved the problem. I was stuck on this for more than a week. Thanks again.
– Jp Brill
Jul 1 at 15:25
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
Student student = Students.FirstOrDefault(x => x.GetFullName().Contains(fullName));
int i = Students.IndexOf(student);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I would start with letting us know which line gives you such error. :)
– mwilczynski
Jul 1 at 14:04