具有类的C#Lambda表达式

时间:2018-07-01 13:57:07

标签: c# linq lambda

我正在阅读包含学生名单的csv,包括姓名,姓氏,班长,成绩,主题,分数。

如果他不存在,我想添加一个新学生,或者如果列表中存在该学生,则只添加主题和分数。下面的代码:

   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());

        }
    }

}

在学生班上:

    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;
        }


    }

我不断收到错误消息,因为它不是委托类型,所以无法转换类型为'Student'的lambda表达式。

有人可以帮忙吗,我现在迷路了。

2 个答案:

答案 0 :(得分:2)

由于您正在使用对Student的引用,因此在列表中搜索它,然后检索其index是没有意义的。 无论如何,您都在使用Student实例,因此请使用LINQ的FirstOrDefault并检索要修改的对象(在这种情况下,要更改SubjectScore)。

您宁愿:

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);

}

答案 1 :(得分:1)

似乎您正在为IndexOf方法传递Lambda表达式,该方法仅支持Item而不支持Lambda。尝试使用Lambda获取Student,然后使用Student获取索引。

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