Both datasets have the same field structure, I need to pass to Dataset 2 the data Dataset 1
that is NOT found in the Dataset 2
.
- The two columns to compare by are:
Dia
andFecha
Assuming the following data:
Data set 1:
Data set 2:
As can be seen, dataset 1 contains the day Thu. on date 01/04/2018 it is not found in dataset 2, therefore I need to pass only that value to the
Dataset 2
.Dataset 2 may contain duplicate data (as you can see it contains twice the day
Lun. 01/01/2018
), BUT only the data that it already contained, previously loaded.Both datasets contain 7 rows, but the end result should be that dataset 2 contains 8 rows after adding Thursday.
I did it in the following way, it is inserting the Jueves
but at the same time it is duplicating all the other data:
for (int indice = 0; indice < dataset1.Tables[0].Rows.Count; indice++)
{
DateTime fecha_ds1 = Convert.ToDateTime(dataset1.Tables[0].Rows[indice]["Fecha"].ToString());
string fecha1 = fecha_ds1.ToString("yyyy/MM/dd");
string dia1 = dataset1.Tables[0].Rows[indice]["dia"].ToString();
for (int indice2 = 0; indice2 < dataset2.Tables[0].Rows.Count; indice2++)
{
DateTime fecha_ds2 = Convert.ToDateTime(dataset2.Tables[0].Rows[indice2]["Fecha"].ToString());
string fecha2 = fecha_ds2.ToString("yyyy/MM/dd");
string dia2 = dataset1.Tables[0].Rows[indic2]["dia"].ToString();
if (!fecha1.Equals(fecha2) && !dia1.Equals(dia2))
{
//Insertar la fila en Dataset 2
break; //y romper el ciclo para volver a tomar
//el siguiente valor del primer ciclo.
}
}
}
So, how can I have a method that only returns the rows that are not in dataset2, and then inserts them?
Note: It may be to return a new dataset/datatable with those missing data.
EDITED:
Using : Visual Studio 2010 and Netframework 4
You can use the LINQ function
Except
to get thoseDataRow
from the first dataset that don't exist in the second.The only thing is that you must define a class that implements the interface
IEqualityComparer<T>
that passesExcept
the logic that it must use to compare the data rows.You can define a class like the following (it can be a nested class for convenience if you wish):
Note that I simplify the date comparison logic, compared to what you already have, in the following 2 ways:
dia
, because from what I can see, if the date matches, then the day will match as well, so it seems redundant to me to check this additional field.Fecha
is already aDateTime
, but that you want to compare this date without taking into account the portion that includes the time, the easiest way to do this is to use the functionDate
of the classDateTime
, which in the form directly gives you the date without the time.Now, with this class defined, you can get the desired result with the following LINQ expression:
Edition
If you add the reference to
System.Data.DataSetExtensions
, as Sergio suggests, then the code can be a bit "cleaner":LINQ:
Edition 2
If you want to take the list of
DataRow
and add them to your second dataset, assuming both datasets have an identical structure, you can do it simply like this:You need to add the reference to System.Data.DataSetExtensions
then you treat it with linq to dataset