D
D
DailyDDose2018-03-04 18:42:42
ASP.NET
DailyDDose, 2018-03-04 18:42:42

Recursively removing subtasks?

There is such a model

public class TaskModel
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int ParentId { get; set; }
}

How to write a controller method to delete this task and all its subtasks?
[HttpDelete]
public ActionResult RemoveTask([FromBody] TaskModel taskData)
{
            // var tasksIds = GetChildrenIds(taskData.id);
            // Db::rawQuery($"DELETE FROM Tasks WHERE Id in {tasksIds}");
            // Db.SaveChanges();
 
            return Json(Db.Tasks);
}
private IEnumerable<int> GetChildrenIds(int parentId)
{
            return Db.Tasks
                .Where(task => task.Id == parentId)
                .Select(task => task.Id);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
igorsmi, 2018-03-05
@DailyDDose

If it's sql then maybe just use cascade delete!?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question