【问题标题】:Can't convert this into VB.net无法将其转换为 VB.net
【发布时间】:2011-08-16 15:06:32
【问题描述】:

我正在尝试将以下内容转换为 vb.net。提前致谢

   Categories.DataSource = objDT.Rows.Cast<DataRow>()
        .Select(r => new { Attendee = r.Field<string>("Attendee"), Item = r.Field<string>("Item") })
        .GroupBy(v => v.Attendee)
        .Select(g => new { Attendee = g.Key, Item = g.ToList() });

这就是我卡住的地方,我尝试了两种不同的方法,但仍然没有任何效果:

Categories.DataSource = objDT.AsEnumerable() _
    .Select(Function(r) New With {.Attendee = r.Field(Of String)("Attendee"), .Item = r.Field(Of String)("Item")}) _
    .GroupBy(Function(v) v.Field(Of String)("Attendee")) _
    .Select(Function(g) Attendee = g.Key)

Categories.DataSource = objDT.Rows.Cast(Of DataRow)().AsEnumerable _
    .Select New Object(){ Function(r As DataRow) Attendee = r.Field(Of String)("Attendee"), Item = r.Field(Of String)("Item")} _
.GroupBy( Function(v) v.Category) _
.Select( Function(g) new { Category = g.Key, Numbers = g.ToList() }

【问题讨论】:

    标签: c# vb.net linq c#-to-vb.net


    【解决方案1】:

    试试这个:

      Categories.DataSource = objDT.Rows.Cast(Of DataRow)().Select(Function(r) New With { _
     .Attendee = r.Field(Of String)("Attendee"), _
     .Item = r.Field(Of String)("Item") _
    }).GroupBy(Function(v) v.Attendee).Select(Function(g) New With { _
     .Attendee = g.Key, _
     .Item = g.ToList() _
    })
    

    对象类(New Object() With {})不同于匿名类型(New With {})。

    您将来可以使用此站点:http://www.developerfusion.com/tools/convert/csharp-to-vb/。它适用于大多数转换。

    【讨论】:

    • 谢谢Matthieu,这个方法奏效了!我会记得试试上面的转化链接。
    【解决方案2】:

    试试下面的

    Categories.DataSource = objDT.Rows.Cast(Of DataRow)() _
        .Select(Function(r) New With {.Attendee = r.Field(Of String)("Attendee"), .Item = r.Field(Of String)("Item")}) _
        .GroupBy(Function(v) v.Attendee) _
        .Select(Function(g) New With { .Attendee = g.Key, .Item = g.ToList()})
    

    【讨论】:

    • 谢谢 JaredPar,您的解决方案也同样有效!我也可以给你绿色的复选标记。谢谢:)
    猜你喜欢
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 2011-09-16
    相关资源
    最近更新 更多