I have the following function that checks if an Id exists in 4 different tables and returns a boolean value:
public bool CheckIfUsed(int targetId)
{
bool isUsedOnTable1 = false;
bool isUsedOnTable2 = false;
bool isUsedOnTable3 = false;
bool isUsedOnTable4 = false;
isUsedOnTable1 = this.DbContext.table1.Select(target => target.TargetID).Where(TargetID => TargetID == targetId).Count() > 0;
isUsedOnTable2 = this.DbContext.table2.Select(target => target.TargetID).Where(TargetID => TargetID == targetId).Count() > 0;
isUsedOnTable3 = this.DbContext.table3.Select(target => target.TargetId).Where(targetID => targetID == targetId).Count() > 0;
isUsedOnTable4 = this.DbContext.table4.Select(target => target.TargetID).Where(targetID => targetID == targetId).Count() > 0;
return (isUsedOnTable1 || isUsedOnTable2 || isUsedOnTable3 || isUsedOnTable4);
}
This approach technically works, the problem here is the performance issues of having 4 different queries each time this function is executed. Is there any way to check the four tables simultaneously or any other way to improve the performance?
Thanks in advance.