Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.Net CsvHelper类地图_Vb.net_Linq_Csv - Fatal编程技术网

VB.Net CsvHelper类地图

VB.Net CsvHelper类地图,vb.net,linq,csv,Vb.net,Linq,Csv,我正在尝试使用CsvHelper库读取VB.Net中的csv文件,但在将行映射到自定义类时遇到问题。与C相反,我如何用VB编写下面的代码# 公共密封类MyClassMap:CsvClassMap { 公共MyClassMap() { Map(m=>m.Id); 地图(m=>m.名称); } } 代码取自 干杯,康纳假设有这样一个物体: Public Class MyObject Public Property Id As Integer Public Property Name

我正在尝试使用CsvHelper库读取VB.Net中的csv文件,但在将行映射到自定义类时遇到问题。与C相反,我如何用VB编写下面的代码#

公共密封类MyClassMap:CsvClassMap
{
公共MyClassMap()
{
Map(m=>m.Id);
地图(m=>m.名称);
}
}
代码取自


干杯,康纳假设有这样一个物体:

Public Class MyObject
    Public Property Id As Integer
    Public Property Name As String
End Class
Public NotInheritable Class MyObjectMap
    Inherits CsvClassMap(Of MyObject)

    Sub New()
        Map(Function(x) x.Id)
        Map(Function(x) x.Name)
    End Sub

End Class
Public Class MyObject
    Public Property Id As Integer
    Public Property Name As String
End Class
Public NotInheritable Class MyObjectMap
    Inherits CsvHelper.Configuration.ClassMap(Of MyObject)
    Sub New()
        Map(Function(x) x.Id)
        Map(Function(x) x.Name)
    End Sub
End Class
您可以这样映射它:

Public Class MyObject
    Public Property Id As Integer
    Public Property Name As String
End Class
Public NotInheritable Class MyObjectMap
    Inherits CsvClassMap(Of MyObject)

    Sub New()
        Map(Function(x) x.Id)
        Map(Function(x) x.Name)
    End Sub

End Class
Public Class MyObject
    Public Property Id As Integer
    Public Property Name As String
End Class
Public NotInheritable Class MyObjectMap
    Inherits CsvHelper.Configuration.ClassMap(Of MyObject)
    Sub New()
        Map(Function(x) x.Id)
        Map(Function(x) x.Name)
    End Sub
End Class

杰夫的回答略有变化:


假设对象是这样的:

Public Class MyObject
    Public Property Id As Integer
    Public Property Name As String
End Class
Public NotInheritable Class MyObjectMap
    Inherits CsvClassMap(Of MyObject)

    Sub New()
        Map(Function(x) x.Id)
        Map(Function(x) x.Name)
    End Sub

End Class
Public Class MyObject
    Public Property Id As Integer
    Public Property Name As String
End Class
Public NotInheritable Class MyObjectMap
    Inherits CsvHelper.Configuration.ClassMap(Of MyObject)
    Sub New()
        Map(Function(x) x.Id)
        Map(Function(x) x.Name)
    End Sub
End Class
您可以这样映射它:

Public Class MyObject
    Public Property Id As Integer
    Public Property Name As String
End Class
Public NotInheritable Class MyObjectMap
    Inherits CsvClassMap(Of MyObject)

    Sub New()
        Map(Function(x) x.Id)
        Map(Function(x) x.Name)
    End Sub

End Class
Public Class MyObject
    Public Property Id As Integer
    Public Property Name As String
End Class
Public NotInheritable Class MyObjectMap
    Inherits CsvHelper.Configuration.ClassMap(Of MyObject)
    Sub New()
        Map(Function(x) x.Id)
        Map(Function(x) x.Name)
    End Sub
End Class

对于我拥有的新版本(24.0.1),下面的代码将用于CsvHelper的映射

Dim textReader As TextReader = File.OpenText(filename)

Dim config = New CsvHelper.Configuration.CsvConfiguration(System.Globalization.CultureInfo.InvariantCulture)
        config.Delimiter = ","
        config.MissingFieldFound = Nothing
        config.TrimOptions = True
        config.HeaderValidated = Nothing

Dim csv = New CsvReader(textReader, config)

csv.Context.RegisterClassMap(Of MyObject)()
这里是map类的一个示例

Public Class MyObject
    Public Property Handle As String
    Public Property Title As String
    Public Property BodyHTML As String
    Public Property Vendor As String
    Public Property Type As String
    Public Property Tags As String

End Class

Public NotInheritable Class ModelMyObjectMap
    Inherits CsvHelper.Configuration.ClassMap(Of MyObject)

    Public Sub New()
        Map(Function(m) m.Handle).Name("Handle")
        Map(Function(m) m.Title).Name("Title")
        Map(Function(m) m.BodyHTML).Name("Body (HTML)")
        Map(Function(m) m.Vendor).Name("Vendor")
        Map(Function(m) m.Type).Name("Type")
        Map(Function(m) m.Tags).Name("Tags")
   End Sub
End Class

您需要显示主类(MyClass,它不是合法名称)欢迎使用堆栈溢出。你能不能更具体地说一下什么已经改变,以及如何使用新版本?我只是修改了答案,但是,重要的一行是一行!