I have a form that contains a list of objects that are all updated at once. In the controller, a nested loop compares each model to the other models so it can determine which objects have been updated and which are new.
foreach(var x in formObject)
{
foreach(var y in dbObject)
{
if(x.id == y.id)
{
//compare each property and notify user of change if different
}
}
}
This takes a huge amount of time considering the large number of objects and how slow the servers are.
Is there a better practice for doing this? Some way of determining which objects have been updated before looping through all? Or a more efficient loop?
You could use a join in Linq:
var differences =
from x in formObject
join y in dbObject on x.id equals y.id
where ( /* compare each property */ )
select new { /* what do you want to tell the user */ };
Of course, without more information about what's in the loop, that's just about all the code I could provide.
In fluent syntax that would be:
var differences =
formObject.Join(dbObject, x => x.id, y => y.id, (x, y) => new { x, y })
.Where(p => /* compare properties of p.x and p.y */)
.Select(p => new { /* what do you want to tell the user */ });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With