Comparing objects between 2 different lists to determine which have different properties.

I have a list of EventType objects with the following definition.

	
public class EventType
{
	public int EventTypeId { get; set; }

	public string EventTypeDesc { get; set; }

	public DateTime CreateDate { get; set; }

	public int SourceProcessId { get; set; }

	public double DecisionWeight { get; set; }
}
	

These are used in a web application where the properties can be adjusted and the resulting list is posted back to the controller action. Rather than updating all the objects returned (even the non-modified ones) I wanted to extract only the objects with a modified DecisionWeight value. The LINQ Except method is the obvious choice for this but in order to get it working for custom objects you need to implement IEquatable. As I’m only interested in the change in one value of the object this seemed a bit of overkill and so I managed to extract the updated objects using the following LINQ.

	
[HttpPost]
public IActionResult NonTransactional(List eventTypes)
{
	// Get original values
	List originalEventTypes = Common.GetEventType(_config.GetConnectionString("DataConnection"));

	// Get updated values
	List updatedEventTypes = eventTypes.Where(x => !originalEventTypes.Any(y => y.EventTypeId == x.EventTypeId && y.DecisionWeight == x.DecisionWeight)).ToList();

	foreach (EventType eventType in updatedEventTypes)
	{
		Common.SetEventType(_config.GetConnectionString("DataConnection"), eventType);
	}
	
	return View(eventTypes);
}
	

Leave a Reply

Your email address will not be published. Required fields are marked *