0

Multiple answers have led me to the following 2 solutions, but both of them do not seem to be working correctly.

What I have are 2 objects

public class DatabaseAssignment : AuditableEntity
{
    public Guid Id { get; set; }
    public string User_Id { get; set; }
    public Guid Database_Id { get; set; }       
}

public class Database : AuditableEntity
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Server { get; set; }
    public bool IsActive { get; set; }
    public Guid DatabaseClientId { get; set; }
}

Now, the front-end will return all selected Database objects (as IEnumerable) for a given user. I am grabbing all current DatabaseAssignments from the database for the given user and comparing them to the databases by the Database.ID property. My goal is to find the DatabaseAssignments that I can remove from the database. However, my solutions keep returning all DatabaseAssignments to be removed.

if (databases != null)
{
    var unitOfWork = new UnitOfWork(_context);
    var userDatabaseAssignments = unitOfWork.DatabaseAssignments.GetAll().Where(d => d.User_Id == user.Id);

    //var assignmentsToRemove = userDatabaseAssignments.Where(ud => databases.Any(d => d.Id != ud.Database_Id));
    var assignmentsToRemove = userDatabaseAssignments.Select(ud => userDatabaseAssignments.FirstOrDefault()).Where(d1 => databases.All(d2 => d2.Id != d1.Database_Id));
    var assignmentsToAdd = databases.Select(d => new DatabaseAssignment { User_Id = user.Id, Database_Id = d.Id }).Where(ar => assignmentsToRemove.All(a => a.Database_Id != ar.Database_Id));

    if (assignmentsToRemove.Any())
    {   
       unitOfWork.DatabaseAssignments.RemoveRange(assignmentsToRemove);
    }
    if (assignmentsToAdd.Any())
    {
       unitOfWork.DatabaseAssignments.AddRange(assignmentsToAdd);
    }
    unitOfWork.SaveChanges();
}

1 Answer 1

1

I think u are looking for an Except extension, have a look at this link

LINQ: Select where object does not contain items from list

Or other way is with contains see below Fiddler link :

https://dotnetfiddle.net/lKyI2F

Sign up to request clarification or add additional context in comments.

4 Comments

I followed the answer on this SO and still have the same problem. var databaseIds = databases.Select(d => d.Id).ToList(); var assignmentsToRemove = userDatabaseAssignments.Where(ud => !databaseIds.Contains(ud.Database_Id)); AssignmentsToRemove still contains objects with matching Database_Id's.
What list is databaseids? Is it the collection of ids you want or the ones you don't want? Try select ids which you don't want into a list then replace above with: userDatabaseAssigbments.Where(lst => !idsDontwant.Contains(lst.Id)).ToList(); I'm not on my laptop so can't test anything.
databaseids is the list of database ids I want since I'm returning the selected items from the front end. So, I need to get the databaseassignments that do not contain the id's in that list in order to remove them.
I have updated my answer and added a fiddle link for you, please check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.