Convert Linq Query Result to Dictionary
Try the following
Dictionary<int, DateTime> existingItems =
(from ObjType ot in TableObj).ToDictionary(x => x.Key);
Or the fully fledged type inferenced version
var existingItems = TableObj.ToDictionary(x => x.Key);
Try using the ToDictionary
method like so:
var dict = TableObj.Select( t => new { t.Key, t.TimeStamp } )
.ToDictionary( t => t.Key, t => t.TimeStamp );
Looking at your example, I think this is what you want:
var dict = TableObj.ToDictionary(t => t.Key, t=> t.TimeStamp);
Use namespace
using System.Collections.Specialized;
Make instance of DataContext
Class
LinqToSqlDataContext dc = new LinqToSqlDataContext();
Use
OrderedDictionary dict = dc.TableName.ToDictionary(d => d.key, d => d.value);
In order to retrieve the values use namespace
using System.Collections;
ICollection keyCollections = dict.Keys;
ICOllection valueCollections = dict.Values;
String[] myKeys = new String[dict.Count];
String[] myValues = new String[dict.Count];
keyCollections.CopyTo(myKeys,0);
valueCollections.CopyTo(myValues,0);
for(int i=0; i<dict.Count; i++)
{
Console.WriteLine("Key: " + myKeys[i] + "Value: " + myValues[i]);
}
Console.ReadKey();