How to filter and group records efficiently in C#

  Kiến thức lập trình

A table Work has three columns: ID, Region and Worker.

And there are two DropDown lists.

DropDown A: Select and filter the Region, is bind to region with data regions

DropDown B: Select and filter the Worker, is bind to worker with data workers

The flow: user selects region, then selects worker of that region

Initialize:

List<Work> works = new(); 
works = GetWorks(); //get all the records from the table
List<string> regions = new();
List<string> workers = new();
regions = works.select(w=>w.region).Distinct().ToList();

When the user selects a region

workers = works.Where(w=>w.Region == region).Select(w=>w.worker).Distinct().ToList();

However, it would be incredibly slow when there are thousands of records.

How can I achieve this more efficently? Thanks!

LEAVE A COMMENT