用于创建表结果的 Lambda 表达式

发布于 2024-10-18 10:23:33 字数 934 浏览 2 评论 0 原文

我有一个数据列表,想要在 GUI (HTML) 中显示为表格。

我想创建一个 lambda 表达式,以便将包含 100 个项目的列表分为 20 行,每行 5 个项目。我可以创建一个简洁的 lambda 表达式来执行此操作吗?

这就是我想到的(是的,5 是一个神奇的数字,它是每行的项目数):

bool isCreateNewRow = true;

var aggregate = Model.Aggregate(new Collection<ICollection<Album>>(), 
                (tableCollection, album) =>
                  {
                      if (isCreateNewRow)
                      {
                          tableCollection.Add(new Collection<Album>());
                          isCreateNewRow = false;
                      }
                      tableCollection.Last().Add(album);
                      if(tableCollection.Last().Count() >= 5)
                      {
                          isCreateNewRow = true;
                      }
                      return tableCollection;
                  });

是否有更短的方法来创建二维数据结构(IEnumerables 的 IEnumerables)?

I have a list of data that I want to display as a table in a GUI (HTML).

I want to create a lambda expression so that a list of hundred items is for instance divided into 20 rows, 5 items each. Can I create a concise lambda expression to do this?

This is what I came up with (yes, 5 is a magic number, it is the number of items per row):

bool isCreateNewRow = true;

var aggregate = Model.Aggregate(new Collection<ICollection<Album>>(), 
                (tableCollection, album) =>
                  {
                      if (isCreateNewRow)
                      {
                          tableCollection.Add(new Collection<Album>());
                          isCreateNewRow = false;
                      }
                      tableCollection.Last().Add(album);
                      if(tableCollection.Last().Count() >= 5)
                      {
                          isCreateNewRow = true;
                      }
                      return tableCollection;
                  });

Is there a shorter way to create a 2 dimensional datastructure (IEnumerables of IEnumerables)?

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

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

发布评论

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

评论(2

云仙小弟 2024-10-25 10:23:33

会容易得多

a) 创建您的(一维)结果集
b) 使用 1 或 2 个 for 循环以表格形式处理(呈现)这些结果。

还因为这两个步骤在逻辑上属于解决方案的不同层。

It would be so much easier to

a) create your (1D) resultset
b) use 1 or 2 for loops to process (present) those results in table form.

Also because those 2 steps would logically belong to different layers of a solution.

我乃一代侩神 2024-10-25 10:23:33
// Create 20 dummy items.
var albums = Enumerable.Range(1, 20)
    .Select(i => string.Format("Album {0}", i));

// Associate each one with the row index.
var numbered = albums
    .Select((item, index) =>
        new { Row = index / 3, Item = item });

// Arrange into rows.
var table = numbered
    .GroupBy(x => x.Row)
    .Select(g => g.Select(x=>x.Item).AsEnumerable());

此时,table 是一个IEnumerable>

要将其转换为 HTML,请尝试以下操作:

var html = rows.Aggregate(
    new StringBuilder("<table>"),
    (tableBuilder, row) => {
        tableBuilder.AppendFormat("<tr>{0}</tr>",
            row.Aggregate(new StringBuilder(),
            (rowBuilder, cell) => {
                rowBuilder.AppendFormat("<td>{0}</td>", cell);
                return rowBuilder;
            }));
        return tableBuilder;
    },
    (tableBuilder) => {
        tableBuilder.Append("</table>");
        return tableBuilder;
    });
// Create 20 dummy items.
var albums = Enumerable.Range(1, 20)
    .Select(i => string.Format("Album {0}", i));

// Associate each one with the row index.
var numbered = albums
    .Select((item, index) =>
        new { Row = index / 3, Item = item });

// Arrange into rows.
var table = numbered
    .GroupBy(x => x.Row)
    .Select(g => g.Select(x=>x.Item).AsEnumerable());

At this point, table is an IEnumerable<IEnumerable<string>>.

To turn it into HTML, try this:

var html = rows.Aggregate(
    new StringBuilder("<table>"),
    (tableBuilder, row) => {
        tableBuilder.AppendFormat("<tr>{0}</tr>",
            row.Aggregate(new StringBuilder(),
            (rowBuilder, cell) => {
                rowBuilder.AppendFormat("<td>{0}</td>", cell);
                return rowBuilder;
            }));
        return tableBuilder;
    },
    (tableBuilder) => {
        tableBuilder.Append("</table>");
        return tableBuilder;
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文