Jaeriko Jaeriko - 1 year ago 82
C# Question

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)
deleteFlag = 1;
return deleteFlag;// if found count = 1 == true
return deleteFlag;

However, the
won't work, giving me the error

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
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

Answer Source

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download