Hello everyone,
Currently I'm having an issue with checking if two entities are the same.
I am using LLBLGen Pro 5.1, Self Servicing with the LLBLGen Runetime Framework.
I have a MSSQL database table with the following values making it unique:
LivingroomId (int)
EmployeeId (int)
Date (date)
ShiftTypeEnum (byte)
What am I trying to do, I have ListBoxes, where employees can be selected, and on the Edit I want to change whatever has been changed (new people added, or old people removed).
So I got an original collection of LivingroomShiftEventEntities and a new collection of LivingroomShiftEntities. What I'd like to do is to see the difference and update the database based on that. But when I do a Contains(), Equals or == check it gives false where I expect a true.
(Attachment of watch screenshot is waiting for approval, so for now I just typed it)
Watch output:
originalCollection[0] == shift // output: false
originalCollection[0] // output: {AmstaJanBonga.Business.EntityClasses.LivingroomShiftEventEntity}
shift // output: {AmstaJanBonga.Business.EntityClasses.LivingroomShiftEventEntity}
shift.LivingroomId // output: 2
originalCollection[0].LivingroomId // output: 2
shift.EmployeeId // output: 2
originalCollection[0].EmployeeId // output: 2
shift.Date // output: {9-5-2017 0:00:00 }
originalCollection[0].Date // output: {9-5-2017 0:00:00}
shift.ShiftTypeEnum // output: 0
originalCollection[0].ShiftTypeEnum // output: 0
shift.Equals(originalCollection[0]) // output: false
shift.GetType() // output: {Name = "LivingroomShiftEventEntity" FullName = "..."}
originalCollection[0].GetType() // output: {Name = "LivingroomShiftEventEntity" FullName = "..."}
The code I'm working with (not final code, experimenting to see what's working):
public static void UpdateMulti(LivingroomShiftEventCollection originalCollection, List<LivingroomShiftEventEntity> shifts)
{
// The new shift collection, could be identical to the existing originalCollection,
// but could also be completely different.
var newCollection = new LivingroomShiftEventCollection();
newCollection.AddRange(shifts);
var shiftsToDelete = new LivingroomShiftEventCollection();
foreach (var shift in originalCollection)
{
// The shift doesn't exist in the new collection, thus should be removed.
if (!newCollection.Contains(shift))
{
shiftsToDelete.Add(shift);
}
}
foreach (var shift in newCollection)
{
// There was a new shift added, thus it should be added.
if (!originalCollection.Contains(shift))
{
shift.Save();
}
}
foreach (var shift in shiftsToDelete)
{
shift.Delete();
}
}
As you can see, the output is the same, so how come it comes out as false when I do a check? Do I need to override the equals if I wanna do checks based on what makes it unique in the database?
Attachments
Filename |
File size |
Added on |
Approval |
equals.PNG
|
24,688 |
09-May-2017 11:59.53 |
Approved |