将列表分成C#块

时间:2017-01-25 01:16:46

标签: c# generic-list programmatically reorderlist

我有一个分支列表,每个都有一个N号员工,我有一个Branch对象和一个NumberEmployees属性,现在我需要遍历该列表发送每个块的员工数量,我用以下内容更好地解释table:我按员工人数订购List,到目前为止没有问题。

+---------+-----------+
| Branch  | Employees |
+---------+-----------+
|MEXICO   |     800   |
|USA      |     700   |
|INDIA    |     500   |
|CHINA    |     400   |
|AUSTRALIA|     300   |
+---------+-----------+

现在遍历一个列表,但将员工数量划分为以下块:

+-----------+------------+-------------+------------+
|  Branch   | FirstGroup | SecondGroup | ThirdGroup |
+-----------+------------+-------------+------------+
| Mexico    |        267 |         267 |        267 |
| USA       |        234 |         234 |        234 |
| India     |        167 |         167 |        167 |
| China     |        134 |         134 |        134 |
| Australia |        100 |         100 |        100 |
+-----------+------------+-------------+------------+

最后我认为应该产生的清单是:

+-----------+-----------+
|  Branch   | Employees |
+-----------+-----------+
| Mexico    |       267 |
| USA       |       234 |
| India     |       167 |
| China     |       134 |
| Australia |       100 |
| Mexico    |       267 |
| USA       |       234 |
| India     |       167 |
| China     |       134 |
| Australia |       100 |
| Mexico    |       267 |
| USA       |       234 |
| India     |       167 |
| China     |       134 |
| Australia |       100 |
+-----------+-----------+

到目前为止,我只能订购清单。

double TotalEmployees = ListBranch.Sum(item => item.EmployeeNumber);
double blockSize = TotalEmployees / ListBranch.Count();
double sizeQuery = Math.Ceiling(blockSize);

                foreach (Branch branch in ListBranch.OrderByDescending(f => f. EmployeeNumber))
{

    //to do
}

感谢您为我能提供的任何线索提供的宝贵帮助

1 个答案:

答案 0 :(得分:0)

这可能会为你做到这一点

List<BranchEmployee> be = new List<BranchEmployee>();
be.Add(new BranchEmployee() { Branch = "MEXICO", Employee = 800 });
be.Add(new BranchEmployee() { Branch = "USA", Employee = 700 });
be.Add(new BranchEmployee() { Branch = "INDIA", Employee = 500 });
be.Add(new BranchEmployee() { Branch = "CHINA", Employee = 400 });
be.Add(new BranchEmployee() { Branch = "AUSTRALIA", Employee = 300 });
List<BranchEmployee> ExpectedBE = new List<BranchEmployee>();
for(int i = 0; i <= 2; i++)
{
    foreach(BranchEmployee smbe in be)
    {
        ExpectedBE.Add(new BranchEmployee()
        {
            Branch = smbe.Branch,
            Employee = smbe.Employee / 3
        });
    }
}

我看到的是,每个员工的员工人数相等,即员工总数除以3。

要按照您展示的方式查看数据,我已创建了一个类似

的类
public class BranchEmployee
{
    public string Branch { get; set; }
    public int Employee { get; set; }
}