我的數據庫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
我不明白的字典部分。 – OneFineDay 2015-03-13 14:31:14
詞典有一個鍵和一個值。你有3個屬性,那麼如何排隊呢?告訴我們你在做什麼,我們可以提供幫助。 – OneFineDay 2015-03-13 14:34:58
這對密鑰應該是數據庫字段名稱作爲密鑰,並且該值應該是該字段的DB值。 – 2015-03-13 17:23:35