The detail that I see in your code is that for each item in list 1 you are going through completely list 2, because your if what it does is identify that they are equal in the Art_CodGen and that they are of different price, to finally update, this does not stop the foreach of the list 2 and continues iterating objects as well as it continues evaluating and affecting the performance of the application. I will leave you the fragment of code that I occupy in these cases and I explain it to you in detail below:
foreach (persona pItem in lista1)
{
persona perAux = lista2.FirstOrDefault(x => x.Codigo == pItem.Codigo);
if (perAux != null)
{
//Actualizas
Console.WriteLine("La persona: " + perAux.Nombre + " esta en la lista dos");
}
}
Now I explain the scenario you set, I have defined a person-type object with two attributes, you know to simplify things:
public class persona
{
public int Codigo { get; set; }
public string Nombre { get; set; }
}
Two person type lists are created:
List<persona> lista1 = new List<persona>();
List<persona> lista2 = new List<persona>();
Objects are added to the list where, eye three of the people will be on both lists ( Katz , Manuel y Uriel
).
//Personas que iran en la lista 1
persona p1 = new persona() { Codigo = 1,Nombre="Katz" };
persona p2 = new persona() { Codigo = 2, Nombre = "Katz" };
persona p3 = new persona() { Codigo = 3, Nombre = "Arturo" };
persona p4 = new persona() { Codigo = 4, Nombre = "Moises" };
persona p5 = new persona() { Codigo = 5, Nombre = "Manuel" };
persona p6 = new persona() { Codigo = 6, Nombre = "Uriel" };
lista1.Add(p1);
lista1.Add(p2);
lista1.Add(p3);
lista1.Add(p4);
lista1.Add(p5);
lista1.Add(p6);
//Personas que iran en la lista 2
persona p7 = new persona() { Codigo = 1, Nombre = "Katz" };
persona p8 = new persona() { Codigo = 5, Nombre = "Manuel" };
persona p9 = new persona() { Codigo = 6, Nombre = "Uriel" };
persona p10 = new persona() { Codigo = 7, Nombre = "Ana" };
persona p11 = new persona() { Codigo = 8, Nombre = "Rosa" };
persona p12 = new persona() { Codigo = 9, Nombre = "Luisa" };
lista2.Add(p7);
lista2.Add(p8);
lista2.Add(p9);
lista2.Add(p10);
lista2.Add(p11);
lista2.Add(p12);
Once the scenario has been defined, if I explain the code to you, a single foreach is made that goes through the person objects in the first list, inside the foreach of the first list an auxiliary person object is created, now this is the part where you should pay attention:
lista2.FirstOrDefault(x => x.Codigo == pItem.Codigo);
A search is performed using the function FirstOrDegault
and a lambda expression (very useful) where it is evaluated that the code (ID) of the pItem object in list 1 is in list two:
x => x.Codigo == pItem.Codigo
If you find someone like them in list 2, the perAux object will not be null and you can also do your other if (the price one), because the perAux is the one that you extracted from the list 2 and the pItem is the one from the list 1 that is, you have the two objects for Do the logic you require .
foreach (persona pItem in lista1)
{
persona perAux = lista2.FirstOrDefault(x => x.Codigo == pItem.Codigo);
if (perAux != null)
{
//Actualizas
Console.WriteLine("La persona: " + perAux.Nombre + " esta en la lista dos");
}
}
This is the output of the program.
La persona: Katz esta en la lista dos
La persona: Manuel esta en la lista dos
La persona: Uriel esta en la lista dos