andrelange91 andrelange91 - 1 year ago 93
C# Question

Check id from two tables, if there is no match delete it

i need to check on TimeEntries from two different databases.

one is where we log our hours(LogDB), the other is for managing those hours(ManagerDB).

so if the timeEntry is removed from where we log our hours, it needs to be removed from our manager as well.

but how do i check this ?

here i get my data :

// get TimeEntries from ManagerDB, add timespan of 10 years.
var DBTimeEntries = GetTimeEntriesFromDB(new TimeSpan(3650, 0, 0, 0));

(returns a list of ID's)

// get TimeEntries from LogDB, add timespan of 10 years.
var TickTimeEntries = GetTickTimeEntries<TimeEntry>(new TimeSpan(3650, 0, 0, 0)).Select(x=>;

(returns a list of ID's)

and here im trying to check if there is no match.

foreach (InternExternalId te in DBTimeEntries)
{// Manager Data:
foreach (int t in TickTimeEntries)
{// Logger data.

if (te.TickId != t)
delete method here...


it should only delete the data fra ManagerDB, if there is no match what so ever, meaning it has been deleted from LogDB.

Answer Source

Hope I got your scenario right. You could use linq instead of the nested foreach statments:

var idsToRemove = TickTimerEntries.Except(DBTimeEntries.Select(t => t.TickId)); 
// Entries that existed in LogDB but not in ManagerDB no populate idsToRemove.
// You should now delete the entries which appear in idsToRemove
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download