C
C#2y ago
SWEETPONY

✅ How to get ids from tuple list?

I have following list: List<(string Path, DepartmentDto Department)> All I want is call following method with departments ids as parameter: await _subjectClient.GetByDepartmentIds(); I can do smth like this:
foreach(var department in departments)
{
var id = department.Department.Id;
await _subjectClient.GetByDepartmentIds(id );
}
foreach(var department in departments)
{
var id = department.Department.Id;
await _subjectClient.GetByDepartmentIds(id );
}
but it's not optimized how to do better?
10 Replies
ero
ero2y ago
you can't possibly care about optimization with all the other logic around it
SWEETPONY
SWEETPONYOP2y ago
I can
FusedQyou
FusedQyou2y ago
var ids = departments.Select(x => x.Department.Id);
var tasks = ids.Select(x => _subjectClient.GetByDepartmentIds(x);
foreach(var task in tasks)
{
var whatever = await task;

// Do whatever you want with whatever `GetByDepartmentIds` is supposed to return
}
var ids = departments.Select(x => x.Department.Id);
var tasks = ids.Select(x => _subjectClient.GetByDepartmentIds(x);
foreach(var task in tasks)
{
var whatever = await task;

// Do whatever you want with whatever `GetByDepartmentIds` is supposed to return
}
Untested but I doubt there is much to "optimize" in your example in general without sacrificing readability. This runs in parallel to be the quickest. It might not be what you want. I'm sure there is even a "better" way for this parallel version.
ero
ero2y ago
then clean that up first
SWEETPONY
SWEETPONYOP2y ago
I don't want to call GetByDepartmentIds a lot of times
FusedQyou
FusedQyou2y ago
I don't know your logic behind the code though So I would not know how to optimize it You could change it to allow multiple ids, and return a list of results
SWEETPONY
SWEETPONYOP2y ago
Task<MqttResult<IList<SubjectDto>>> GetByDepartmentsIds( IEnumerable<Guid> departmentsIds, RequestParameters parameters = default ); it's already can do it look, I have list of departments grouped by title so I wanna get subjects for each department without multiple call and without destroying grouping now I have this: List<(string Path, DepartmentDto Department)> I want this: List<(string Path, List<SubjectDto>)> or smth like that
Anton
Anton2y ago
_subjectClient.GetByDepartmentIds(departments.Select(d => d.Department.Id)); ah I see what you mean well, after the call, group again on the department id of the subject
Mayor McCheese
I’m probably not following well; why not use an indexed collection?
SWEETPONY
SWEETPONYOP2y ago
I decided to rewrite my algorithm and now everything works
Want results from more Discord servers?
Add your server