With c# and LinqToSql I query a database Table and return a single unique record.
QUESTION: How do I place the results of my query into a Dictionary with: the Column Names of the database Table as the Dictionary Keys and the Column Values of the database Table as the Dictionary Values.
Note: I will not always know the name or number of the Table's Column Names
Dictionary<string, string> QueryResult = new Dictionary<string, string>
var UniqueRecord = (from tbl DataContext.Table
where tbl.ColumnName1.Equals(Parameter1)
&& tbl.ColumnName2.Equals(Parameter2)
select tbl);
//How do I add var UniqueRecord to the Dictionary
==================================================================================
==================================================================================
var UniqueRecord = (from tbl DataContext.Table
where tbl.ColumnName1.Equals(Parameter1)
&& tbl.ColumnName2.Equals(Parameter2)
select tbl);
foreach(var item in OnlineBezeichnungRekord)
{
var properties = item.GetType()
.GetProperties()
.ToDictionary(p => p.Name, p => p.GetValue(item, null).ToString());
Dictionary<string,string> DictionaryValues = properties;
}
Considering how simple it is to convert a DataRow
into a Dictionary
manually, I would not go with Enumerable.ToDictionary
in your case, and just do loops:
Private Function GetDictionaryFromDataRow(dr As DataRow) As Dictionary(Of String, Object)
Dim dict As New Dictionary(Of String, Object)
For Each col As DataColumn In dr.Table.Columns
dict.Add(col.ColumnName, dr.Item(col))
Next
Return dict
End Function
Then just make sure your query returns a DataRow
, so use UniqueRecord(0)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.