C# 循环遍历集合并将每个对象分配给变量

发布于 2024-11-03 20:57:47 字数 440 浏览 0 评论 0原文

我确信这很简单,但我似乎无法做到这一点。 我建立了一个用户 ICollection。该集合可以有 1 个或多个。 我现在想要循环遍历这个集合,并为每个用户分配一个变量,然后我可以用它来填写电子表格。

目前我有这段代码:

    string username = null;

        foreach (var user in dailyReport)
        {
             username = user.UserName;
        }

        Cell cell= worksheet.Cells["B5"];
        cell.PutValue(username);

现在显然这只是将集合的最后一个用户放入单元格 B5 中! 如何收集所有 user.UserNames,以便将它们放置在 B5、B6、B7 等中???

I am sure this is quite simple but I cannot seem to get this right.
I have a built up a ICollection of Users. This collection could have 1 or many.
I now want to loop through this collection and assign a variable to each user that I can then use to fill in a spread sheet.

At present I have this code :

    string username = null;

        foreach (var user in dailyReport)
        {
             username = user.UserName;
        }

        Cell cell= worksheet.Cells["B5"];
        cell.PutValue(username);

Now obviously this just puts the last user of the collection into cell B5!
How do I collect all user.UserNames so I can place them in B5, B6, B7 and so on????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

为你拒绝所有暧昧 2024-11-10 20:57:47

要获取用户名列表:

List<string> userNames = dailyReport.Select( x=> x.UserName).ToList();

或者,您可以直接在循环中进行分配:

    int index = 5;
    foreach (var user in dailyReport)
    {
         Cell cell= worksheet.Cells["B"+ index.ToString()];
         cell.PutValue(user.UserName);
         index++;
    }

To get a list of the user names:

List<string> userNames = dailyReport.Select( x=> x.UserName).ToList();

Alternatively you can just do the assignment in the loop directly:

    int index = 5;
    foreach (var user in dailyReport)
    {
         Cell cell= worksheet.Cells["B"+ index.ToString()];
         cell.PutValue(user.UserName);
         index++;
    }
忱杏 2024-11-10 20:57:47

您需要将值放入 foreach 循环内部。您要求的具体事情(为集合中的每个值获取不同的变量)是不可能的,而且也不是您想要的。

string column = "B"; // or whatever your column is
int row = 1; // or whatever your starting value is

foreach (var user in dailyReport)
{
    string username = user.UserName;
    string cellAddress = String.Format("{0}{1}", column, row);
    Cell cell = worksheet.Cells[cellAddress];

    cell.PutValue(username);

    row++; // make sure to increment the row, or every username goes in the same cell
}

You need to put the value inside the foreach loop. The specific thing you are asking for--getting a different variable for every value in the collection--is not possible, and isn't what you want anyway.

string column = "B"; // or whatever your column is
int row = 1; // or whatever your starting value is

foreach (var user in dailyReport)
{
    string username = user.UserName;
    string cellAddress = String.Format("{0}{1}", column, row);
    Cell cell = worksheet.Cells[cellAddress];

    cell.PutValue(username);

    row++; // make sure to increment the row, or every username goes in the same cell
}
杀お生予夺 2024-11-10 20:57:47
int i = 5;
foreach (var user in dailyReport)
{
    worksheet.Cells["B" + (i++).ToString()].PutValue(user.UserName);
}
int i = 5;
foreach (var user in dailyReport)
{
    worksheet.Cells["B" + (i++).ToString()].PutValue(user.UserName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文