Search code examples
c#mongodbdaodata-access-layer

c# MongoDB Repo reference for Delete(string id) function won't accept being passed string DAO parameter


I'm trying to create a Delete function in a DAL EmployeeDAO, as seen here:

public long Delete(string Id)
    {
        HelpdeskRepository repo = new HelpdeskRepository(new DbContext());
        long deleteFlag = 0;
        if (Id.Count() > 0)
        {
            repo.Delete(Id.GetIdAsString());
            deleteFlag = 1;
            return deleteFlag;// if found count = 1 == true
        }
        else
        {
            return deleteFlag;
        }
    }

However, the repo.Delete(Id.GetIdAsString()); won't work, giving me the error

Cs1061 Error CS1061 'string' does not contain a definition for 'GetIdAsString' and >no extension method 'GetIdAsString' accepting a first argument of type 'string' >could be found

The Repository (repo) function it is referencing is thus:

public long Delete<HelpdeskEntity>(string id)
    {
        var filter = new FilterDefinitionBuilder<HelpdeskEntity>().Eq("Id", new ObjectId(id));
        var collection = GetCollection<HelpdeskEntity>();
        var deleteRes = collection.DeleteOne(filter);
        return deleteRes.DeletedCount;
    }

and the GetIdAsString() function:

public string GetIdAsString()
    {
        return this.Id.ToString();
    }

I do not understand why the Id parameter won't work in the repo.Delete call either, as Delete accepts string parameters and Id is of type string.

Any thoughts, or better ways to achieve this? I cannot use Insert/async/await methods of creation for this due to future project requirements relying on repository methods, so it must be done using the repo.Delete function.


Solution

  • May I ask where do you define GetIdAsString() method ? The Id in your Delete(string Id) is already a string and the string class has no extension method GetIdAsString() hence the error occurs.

    And in the Delete<HelpdeskEntity>(string id) the parameter id is already a string so it makes no sense to me to have the GetIdAsString() method. Just use repo.Delete(Id).