I have an Excel where I store the following information that I am going to massively upload to a database using C#:
Before the user can upload this to the database I have to perform a couple of validations (order exists, box exists, etc.), one of the validations that I want to do is to validate that the data entered is not repeated within the Excel (in the example image line 3 is repeated, since it belongs to the same box-bag of the same order) for that I use the following code, it is saved in a DataTable and then it becomes a List:
Class
public class MapaCargaEmpaque
{
public string Orden { get; set; }
public int Caja { get; set; }
public int Bolsa { get; set; }
}
Conversion to List
List<MapaCargaEmpaque> ListadoCarga = datatable.AsEnumerable().Select(m => new MapaCargaEmpaque()
{
Orden = Convert.ToString(m.Field<string>("Orden")),
Caja = Convert.ToInt32(m.Field<string>("Caja").ToString()),
Bolsa = Convert.ToInt32(m.Field<string>("Bolsa").ToString())
}).ToList();
search for repeated
var duplicates = ListadoCarga.GroupBy(ki => new {ki.Orden, ki.Caja, ki.Bolsa })
.Where(g => g.Count() > 1)
.Select(g => g.Key);
When running the last code and reviewing the result I see the following:
How can I modify this code (or something similar) to tell me what # of lines were repeated, so I can tell the user "Line #3-line #5 are repeated".
You can add the Row property to your model, to indicate the row in question
Departure: