I
I
INTERNALINTERFERENCE2021-11-18 11:02:57
C++ / C#
INTERNALINTERFERENCE, 2021-11-18 11:02:57

How to get all children by id list?

I have a table with fields: Id, ParentId It is
necessary to implement multiple editing - that is, I change Id, and all ParentId changes. I wrote the following bad code:

static private IEnumerable<Guid> DeleteDepartmentsWithChildren(
    IEnumerable<Guid> departmentIds,
    DbSet<Department> departmentRepository )
{
var deletedDepartments = new List<Guid>();
foreach ( var i in departmentIds )
            {
                var departmentChildren = departmentRepository.Where( p => p.ParentDepartmentId == i ).ToList();
                foreach ( var child in departmentChildren )
                {
                    var deletedChildrenIds = DeleteDepartmentWithChildren( child.Id, currentOperatorId, departmentRepository );
                    child.MarkDeleted( currentOperatorId );
                    deletedDepartments.AddRange( deletedChildrenIds );
                }
            }
}


I don't like the fact that I'm doing a kveri at every step, this is wrong, I would like to just get all the children for all the existing parents.
These lines can probably be replaced somehow. For example, use join, but I don't know how
foreach ( var i in departmentIds )
                var departmentChildren = departmentRepository.Where( p => p.ParentDepartmentId == i ).ToList();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
INTERNALINTERFERENCE, 2021-11-18
@INTERNALINTERFERENCE

did so

var departmentChildren = departmentRepository.Where(
                dr => departmentIds.Contains( dr.ParentDepartmentId ) ).ToList();

but the problem is that it's long, so it's better to use Join
how to do it right?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question