【问题标题】:Convert LINQ C# to VB.Net将 LINQ C# 转换为 VB.Net
【发布时间】:2014-03-03 12:14:33
【问题描述】:

任何人都可以将此 c# linq 代码转换为 VB 吗?我尝试了 developerfusion,没有成功

   DataTable dt = dt1.Copy();
        var result = dt
            .AsEnumerable()
            .Select(r => r.Field<string>(0))
            .Select(s => string.Join("",
                                 s.Select((x, i) => x == '0'
                                                    ? " "
                                                    : (i + 1).ToString()))
             ).ToList();

我尝试的是

    Dim dt As DataTable = dt1.Copy()
    Dim result = dt.AsEnumerable().[Select](Function(r) r.Field(Of String)(0)).[Select](Function(s) String.Join("", s.[Select](Function(x, i) If(x = "0"c, " ", (i + 1).ToString())))).ToList()

但它显示错误

 Data type(s) of the type parameter(s) in extension method 'Public Function Select(Of S)(selector As System.Func(Of String, S)) As System.Data.EnumerableRowCollection(Of S)' defined in 'System.Data.EnumerableRowCollectionExtensions' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.

我的数据表只有一列,看起来像 第一行 1000000 对于第二行 0010000 从第三行 0100000 等开始,我想要得到的是用空格替换所有 0,用它的位置编号替换所有 1,即;对于第一行,所需的输出类似于(1000000)。对于第二行(0030000)和第三行(0200000)。像这样

【问题讨论】:

  • didn't work 是什么意思??
  • 你投了这么多反对票?
  • 你能解释一下错误吗?不过对我来说似乎很好!
  • 我用我得到的错误更新了我的问题

标签: c# vb.net linq


【解决方案1】:

这是一个工作示例:http://dotnetfiddle.net/LNksSW

您收到错误的原因是您在文件顶部缺少Imports System.Linq

Dim dt as DataTable = new DataTable
dt.Columns.Add("col1")
dt.Rows.Add("1000000")
dt.Rows.Add("0010000")
dt.Rows.Add("0100000")      

Dim result = dt _
    .AsEnumerable() _
    .Select(Function(r) r.Field(Of String)(0)) _
    .Select(Function(s) string.Join("", s.Select(Function(x, i) _
        If(x = "0", "0", (i + 1).ToString())))).ToList()

For Each i As String in result
    Console.WriteLine(i)
Next

【讨论】:

  • 得到错误'扩展方法'Public Function Select(Of S)(selector As System.Func(Of String, S)) As System中的类型参数的数据类型。无法从这些参数推断出“System.Data.EnumerableRowCollectionExtensions”中定义的 Data.EnumerableRowCollection(Of S)'。明确指定数据类型可能会纠正此错误。'
  • @Cronaldo - 你能发布你的数据表的小样本吗?或者最好还是在dotnetfiddle.net 创建一个示例
  • @Cronaldo - 这是一个有效的 dotnetfiddle 示例 - dotnetfiddle.net/VTq4j3
【解决方案2】:

异常是明确告诉您在 Select 中指定数据类型

Dim dt As DataTable = New DataTable()
Dim result = dt.
    AsEnumerable().
    Select(Function(r As DataRow) r.Field(Of String)(0)).
    Select(Function(s As String) String.Join(
               "", s.Select(Function(x As Char, i As Integer)
                                Return If(x = "0"c, " ", (i + 1).ToString())
                            End Function))).
    ToList()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    相关资源
    最近更新 更多