EF6查询ToDictionary,其中Key为FieldName,值为VB.NET中的属性值

时间:2015-03-13 14:18:28

标签: vb.net entity-framework-6

我的数据库

id UserName       UserLevel       UserEmail
-- -------------- --------------- -----------------
 1 JohnDoe        1               johndoe@acme.com
 2 UserTest       2               linda@acme.com

我的查询:

Dim MyQuery = (From U in DB.Users _
               Select U).FirstOrDefault

我需要在VB.NET中完成的是:

字典“KeyValuePair”,输出所有“实体属性名称”和相应的值:

DBFieldName, Value of Row
------------  ----------------
Username    , johndoe
UserLevel   , 1
UserEmail   , johndoe@acme.com

想象一下,如果您需要使用此布局生成文本文件(第一个数字是行号):

1: Username, johndoe
2: UserLevel, 1
3: UserEmail, johndoe@acme.com

到目前为止,我所拥有的是:

Dim MyQuery = (From U in DB.Users _
                   Select U).FirstOrDefault

Dim mydict As EFDictionary = ToEFDictionary(Of Users)(MyQuery)


<System.Runtime.CompilerServices.Extension()> _
Public Function ToEFDictionary(Of T)(ByVal Source As IEnumerable(Of T)) As EFDictionary
    Dim EFDictionary As New EFDictionary
    Dim Item As T
    For Each Item In Source
        Dim Properties = Item.GetType().GetProperties
        For Each p In Properties
            Try
                EFDictionary.Add(p.Name, p.GetValue(Item)) ' Loading Lazy Table, I need to Qualify these Properties before add, otherwise will load all the lazy tables.
            Catch ex As Exception
                Throw New Exception("Error loading Lazy Table")
            End Try
        Next
    Next
    Return EFDictionary
End Function


Public Class EFDictionary
    Inherits Dictionary(Of String, String)
    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub New(ByVal dictionary As IDictionary(Of String, String))
        MyBase.New(dictionary)
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

我编辑过支持匿名类型,它直接从Object继承。 以同样的方式,您可以将它用于每个类,因为它继承自Object,GetType()是一个对象方法。

您可以使用反射。

一个例子:

Imports System.Collections.Generic
Imports System.Reflection

Module Module1

    Sub Main()
        Dim cls As TestClass = New TestClass()

        cls.First = "Test"
        cls.Second = 125

        Dim result As List(Of String) = New List(Of String)()

        result = GetPropertiesNamesAndValues(cls, result)

        Dim product = New With {Key .Name = "paperclips", .Price = 1.29}

        result = GetPropertiesNamesAndValues(product, result)

        Dim count As Integer = 1
        For Each r In result
           Console.WriteLine("{0}, {1}", count, r)
           count += 1
        Next
        Console.ReadLine()
    End Sub

    Public Function GetPropertiesNamesAndValues(cls As Object, res As List(Of String)) As List(Of String)
        Dim props As PropertyInfo() = cls.GetType().GetProperties()

        For Each p As PropertyInfo In props
            res.Add(String.Format("{0} , {1}", p.Name, p.GetValue(cls, Nothing).ToString()))
        Next

       Return res
    End Function
End Module

其中TestClass由一对属性组成:

Public Class TestClass
    Private _first As String
    Private _second As String

    Public Property First As String
        Get
            Return _first
        End Get
        Set(value As String)
            _first = value
        End Set
    End Property

    Public Property Second As String
        Get
            Return _second
        End Get
        Set(value As String)
            _second = value
        End Set
    End Property
End Class

答案 1 :(得分:0)

您需要做的就是通过此List(Of String)循环播放该文件。它必须是您传递给此函数的类的实例。

Public Function GetPropertiesNamesAndValues(Of TClass As Class)(cls As TClass) As List(Of String)
  Dim result As New List(Of String)
  Dim props As PropertyInfo() = cls.GetType().GetProperties()
  For Each p As PropertyInfo In props
    result.Add(String.Format("{0} {1}", p.Name, p.GetValue(cls, Nothing)))
  Next
  Return result
End Function

用法:

Dim user = (From U In DB.Users Where U.UserLevel = 1).FirstOrDefault
If Not user Is Nothing Then
  Dim userInfo = GetPropertiesNamesAndValues(user)
  'print results
End If