Skip to content

Instantly share code, notes, and snippets.

@yngwie74
Created October 11, 2012 22:56
Show Gist options
  • Save yngwie74/3876097 to your computer and use it in GitHub Desktop.
Save yngwie74/3876097 to your computer and use it in GitHub Desktop.
Generar un diccionario directamente desde una consulta linq
// forma tradicional, agregando los elementos uno a uno
private IDictionary<int, DateTime> GetDiccionaryOriginal(int[] keys)
{
var result = new Dictionary<int, DateTime>();
using (var context = DataContextFactory.MyContext)
{
context.MY_RECORDs.Where(r => keys.Contains(r.RECORD_ID))
.ToList()
.ForEach(r => result.Add(r.RECORD_ID, r.LATEST_UPDATE_TS));
return result;
}
}
// forma declarativa, usando un lambda para la selección de llave y otro para el valor.
private IDictionary<int, DateTime> GetDiccionaryNew(int[] keys)
{
using (var context = DataContextFactory.MyContext)
{
return context.MY_RECORDs
.Where(r => keys.Contains(r.RECORD_ID))
.ToDictionary(k => k.RECORD_ID, v => v.LATEST_UPDATE_TS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment