private static void CopyColumnFromFile(ref Boletim boletim, string columnName, DataRow cellValue) { var cellValueAsString = cellValue[columnName].ToString().Trim(); if (string.IsNullOrEmpty(cellValueAsString) || !string.IsNullOrEmpty(cellValueAsString) && !int.TryParse(cellValueAsString, out var numBoletim)) { return; } switch (columnName) { case Constants.colBoletim: boletim.NumBoletim = cellValueAsString; break; default: throw new NotSupportedException(); } }
protected override void ImportRows(DataRowCollection dataRows) { var alunos = new List <Aluno>(); var boletins = new List <Boletim>(); var alunoAdapter = new AlunoAdapter(ConnectionString); var boletimAdapter = new BoletimAdapter(ConnectionString); var existentAlunos = alunoAdapter.GetAlunos(); var existentBoletins = boletimAdapter.GetBoletins(); OnNumberOfRowsToImportDetermined(new NumberOfRowsEventArgs { NumberOfRows = dataRows.Count * 2 + 2 }); foreach (DataRow dataRow in dataRows) { var aluno = new Aluno(); var boletim = new Boletim(); foreach (var columnName in GetColumnNames()) { if (columnName == Constants.colBoletim) { CopyColumnFromFile(ref boletim, columnName, dataRow); } else { CopyColumnFromFile(ref aluno, columnName, dataRow); } } if (existentAlunos.Count > 0) { var existentAluno = existentAlunos.FirstOrDefault(a => a.Username == aluno.Username); if (existentAluno != null) { aluno.IdAluno = existentAluno.IdAluno; } else { existentAluno = existentAlunos.FirstOrDefault(a => a.Nome == aluno.Nome); if (existentAluno != null) { aluno.IdAluno = existentAluno.IdAluno; } } } alunos.Add(aluno); boletim.Aluno = aluno; OnRowTreated(EventArgs.Empty); if (string.IsNullOrEmpty(boletim.NumBoletim) || existentBoletins.Exists(b => b.NumBoletim == boletim.NumBoletim)) { OnRowTreated(EventArgs.Empty); continue; } boletins.Add(boletim); OnRowTreated(EventArgs.Empty); } alunoAdapter.StoreAlunos(alunos); OnRowTreated(EventArgs.Empty); var distinctBoletins = boletins.GroupBy(b => new { b.Aluno.IdAluno, b.NumBoletim }).Select(g => g.First()).ToList(); boletimAdapter.StoreBoletins(distinctBoletins); OnRowTreated(EventArgs.Empty); }