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
==================================================================================
I eventually used this
==================================================================================
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
DataRowinto aDictionarymanually, I would not go withEnumerable.ToDictionaryin your case, and just do loops:Then just make sure your query returns a
DataRow, so useUniqueRecord(0).