0

I have the following linq query

  var temp = (from p in db.BEM_EVT_FULL
                      where (p.date_reception > dt)
                      group p by p.mc_object into g
                      orderby g.Count() descending
                      select new StringIntType
                      {
                          str = g.Key,
                          nbr = g.Count()
                      }).Take(50).ToList();

Instead of casting result to StringIntType i need to use Dictionary with int and string types.

drex drex
  • 205
  • 1
  • 8

2 Answers2

4

You could try something like this:

var temp = (from p in db.BEM_EVT_FULL
            where (p.date_reception > dt)
            group p by p.mc_object into g
            orderby g.Count() descending
            select new
            {
                Key = g.Key,
                Value = g.Count()
            }).Take(50)
              .ToDictionary(x=>x.Key, y=>y.Value);
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
Christos
  • 53,228
  • 8
  • 76
  • 108
0

it looks like there may be an answer for this question here: Convert Linq Query Result to Dictionary

basically, instead of ToList use ToDictionary

.ToDictionary( t => t.Key, t => t.Value);
Community
  • 1
  • 1
JustinC
  • 29
  • 5