【问题标题】:Combining database cells in column B for matching cells in column A in VB.NET组合 B 列中的数据库单元格以匹配 VB.NET 中 A 列中的单元格
【发布时间】:2013-06-03 19:31:45
【问题描述】:

我有一个表格(从 SQL 数据库中检索的数据),格式为:

╔═══════╦══════════════╗
║ Model ║ Manufacturer ║
╠═══════╬══════════════╣
║ A     ║            1 ║
║----------------------║
║ A     ║            2 ║
║----------------------║
║ A     ║            3 ║
║----------------------║
║ B     ║            4 ║
║----------------------║
║ B     ║            5 ║
║----------------------║
║ C     ║            6 ║
║----------------------║
║ D     ║            7 ║
║----------------------║
║ D     ║            8 ║
║----------------------║
║ D     ║            9 ║
║----------------------║
║ D     ║           10 ║
╚═══════╩══════════════╝

当我将数据绑定到<asp:datagrid> 时,每一行都是它自己的<tr>。不过,我需要的是:

╔═══════╦══════════════╗
║ Model ║ Manufacturer ║
╠═══════╬══════════════╣
║ A     ║            1 ║
║       ║            2 ║
║       ║            3 ║
║----------------------║
║ B     ║            4 ║
║       ║            5 ║
║----------------------║
║ C     ║            6 ║
║----------------------║
║ D     ║            7 ║
║       ║            8 ║
║       ║            9 ║
║       ║           10 ║
╚═══════╩══════════════╝

我花了很多时间搜索并尝试了许多不同的东西,其中大部分使用 LINQ。但是我对LINQ的了解和了解很少。我认为(但我可能完全错了),我最接近的来自这个问题/答案:Linq query to combine cell results?。我稍作修改的版本是:

Dim results = table.AsEnumerable().GroupBy(Function(x) x.Field(Of String)("Model")).[Select](Function(grouping) New With {
        .Key = grouping.Key,
        .CombinedModel = grouping.Aggregate(Function(a, b) a + " " + b)
    }).[Select](Function(x) New ModelRecord() With {
        .Manufacturer = x.Key.Manufacturer,
        .Model = x.CombinedModel
    })

但是由于我缺乏 LINQ 知识,我不明白“定义一个具体类型来表示 Row”(这会在我的代码中与 ModelRecord() 产生问题)。

在这一点上,我几乎完全迷失了。我是不是把事情复杂化了?完全错了吗?非常感谢这里的任何帮助。

【问题讨论】:

  • 这个解决方案:codecorner.galanter.net/2011/06/02/string-aggregate-in-linq 对你有用吗?
  • Yuriy,这正是我想要做的。我正在尝试使用该代码(针对我的数据表中的列名进行了修改),但出现错误:Argument not specified for parameter 'index' of Public ReadOnly Default Property Chars(index As Integer) As Char
  • 能否请您发布您修改后的代码并指出错误发生的位置?
  • From oRow In table Group By Manufacturer = oRow.Field(Of String)("Manufacturer") Into Group Select Manufacturer, Model = _ Group.Aggregate(Of StringBuilder)(New StringBuilder, Function(Current As StringBuilder, row As DataRow) _ Current.AppendFormat(",{0}", row.Field(Of String)("Model"))).ToString.Substring(1)() 错误是Select Manufacturer, Model = 部分之后的所有内容(也就是说,Visual Studio 会在之后的所有内容下划线)。
  • 为什么要在ToString.Substring(1)后面加上()

标签: asp.net vb.net linq


【解决方案1】:

This solution 我在 cmets 中提到您的问题有效。感谢您试一试。

【讨论】:

    【解决方案2】:

    我相信这更适合Repeater,而不是使用DataGrid 或GridView,您可以更好地控制数据的格式。然后,您可以将数据加载到带有制造商列表 (Dictionary<Model, List<Manufacturer>>) 的模型字典中。加载对象后,您确定如何在中继器中显示它。

    【讨论】:

    • 每个制造商/模型对是否会成为字典中的记录(模型是值,制造商是键)?
    • 情况正好相反。由于您有多个制造商创建给定模型,因此 Key 是模型,每个 Key 都有一个 Value,它是 List 代表创建该模型的所有制造商。编辑我的答案以澄清这一点。
    • 好吧,我对字典和格式中继器一无所知,但我会看看我能学到什么。谢谢。
    • 如果您对 GridView 更熟悉,您可以在父 GridView 中使用嵌套的 GridView 或 DetailsView,而不是使用 Repeater。
    • 不,我相信中继器将是简单的部分。对我来说更具挑战性的事情是弄清楚如何填充字典,因为我们的实际表有 350 条记录,包含 35 个不同的制造商。
    猜你喜欢
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    相关资源
    最近更新 更多