Skip to content

Object must implement IConvertible #31

@baffourt

Description

@baffourt

In the following example, I get Object must implement IConvertible if entire Column between Column D and Column H is blank

private List<RegularDto> GetStudentScores(ExcelPackage p, List<string> students, out int examCount)
{
            const int examDefRow = 11;
            ExcelWorksheet ws = p.Workbook.Worksheets[1];
            const int studentRowStart = 12;
            var lastColumn = ExcelHelper.GetLastColumn(ws, examDefRow, ws.Dimension.End.Column);
            var lastExamCol = ExcelHelper.GetLastExamColumnNameDirect(lastColumn);
            examCount = lastColumn - 3;
            var data = ws.Extract<RegularDto>()
                .WithProperty(r => r.SR, "A")
                .WithProperty(r => r.StudentID, "B")
                .WithProperty(r => r.StudentName, "C")
                .WithCollectionProperty(s => s.Scores,
                    item => item.ClassExamInfo, 9,
                    item => item.Mark, "D", "H")
                .GetData(studentRowStart, ws.Dimension.End.Row);
            return data.Where(s => !string.IsNullOrEmpty(s.StudentID) && students.Contains(s.StudentID.Trim())).ToList();
 }

How can I read the ones with data and the columns without any data should just have Mark to be null?

Below is how RegularDto class has been defined.

public class RegularDto
    {
        public string SR { get; set; }
        public string StudentID { get; set; }
        public string StudentName { get; set; }
        public List<ScoreDto> Scores { get; set; }
        public double? TotalScore { get { return Scores.Sum(s => s.Mark); } }

    }

    public class ScoreDto
    {
        public string ClassExamInfo { get; set; } //ClassExamID|ExamID|ExamName
        public string ClassExamID
        {
            get
            {
                return ClassExamInfo.IndexOf('|') < 0 ? ClassExamInfo : ClassExamInfo.Split('|')[0];
            }
        }
        public double? Mark { get; set; }
        public string ExamID
        {
            get
            {
                return ClassExamInfo.IndexOf('|') < 0 ? ExamIDInClassExam : ClassExamInfo.Split('|')[1];
            }
        }

        public string ExamName
        {
            get
            {
                return ClassExamInfo.IndexOf('|') < 0 ? "" : ClassExamInfo.Split('|')[2];
            }
        }
        private string ExamIDInClassExam
        {
            get
            {
                var index = ClassExamID.IndexOf("EX");
                return ClassExamID.Substring(index, 4);
            }
        }
    }

If all columns from D to H are blank, then no error will occur but the error only occurs when say column D has some data, maybe column E has some data but the entire column F,G or H is blank.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions