1

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

+---------+-----------+
| 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
}

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

4

1 回答 1

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; }
}
于 2017-01-25T01:30:14.180 回答