2015-03-13 31 views
-1

我的數據庫EF6查詢ToDictionary在VB.NET Key作爲字段名和值屬性值

id UserName  UserLevel  UserEmail 
-- -------------- --------------- ----------------- 
1 JohnDoe  1    [email protected] 
2 UserTest  2    [email protected] 

我的查詢:

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

我需要在VB中實現什麼目標.NET是:

字典「KeyValuePair」輸出所有的「實體屬性名稱」和相應的值:

DBFieldName, Value of Row 
------------ ---------------- 
Username , johndoe 
UserLevel , 1 
UserEmail , [email protected] 

試想一下,如果你需要生成與該佈局的文本文件(第一個數字是行號):

1: Username, johndoe 
2: UserLevel, 1 
3: UserEmail, [email protected] 

到目前爲止that'是我有:

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 
+0

我不明白的字典部分。 – OneFineDay 2015-03-13 14:31:14

+0

詞典有一個鍵和一個值。你有3個屬性,那麼如何排隊呢?告訴我們你在做什麼,我們可以提供幫助。 – OneFineDay 2015-03-13 14:34:58

+0

這對密鑰應該是數據庫字段名稱作爲密鑰,並且該值應該是該字段的DB值。 – 2015-03-13 17:23:35

回答

0

我已經編輯,支持匿名類型,直接從對象繼承。 以同樣的方式,您可以將它用於每個類,因爲它從Object繼承而來,並且GetType()是一個Object Method。

您可以使用反射。

一個例子:

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 
+0

對於靜態類將會工作,但對於Linq來自DB查詢的結果卻沒有。 – 2015-03-13 17:24:18

+0

這是一個很好的方式,但我認爲我們還沒有走上正確的道路。我希望OP會告訴我們爲什麼他們這樣做以及如何使用它。我認爲有一個設計缺陷的poss。也許是匿名類型。 – OneFineDay 2015-03-13 17:28:51

+0

@AngeloBestetti:實體框架使用POCO在內存中的加載數據,DB.Users是在解決方案中的某處定義的類。如果你給我們更多的細節,我們可以明白髮生了什麼問題。 – 2015-03-13 17:38:47

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 
+0

,看你是硬編碼領域的「用戶名,用戶級,USEREMAIL「我需要的是不產生知道了田的名字,像任何查詢,我可以從字典翻譯的名稱。 – 2015-03-13 18:37:51

+0

然後你需要反思。你有沒有嘗試*達尼洛的答案 - 它只是需要一些修改來滿足你的需求。 – OneFineDay 2015-03-13 18:40:43

+0

你不能合併我的答案這個達尼洛的。再看一遍。我有一個用法部分,顯示如何調用它。你不需要一個詞典。 – OneFineDay 2015-03-13 19:10:10

相關問題