用toDictionary查询Linq

时间:2015-04-16 06:58:43

标签: c# asp.net linq entity-framework

我有以下linq查询

  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();

我不需要将结果转换为StringIntType,而是需要使用带有int和字符串类型的Dictionary。

2 个答案:

答案 0 :(得分:4)

您可以尝试这样的事情:

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);

答案 1 :(得分:0)

在这里看起来可能有这个问题的答案:Convert Linq Query Result to Dictionary

基本上,而不是ToList使用ToDictionary

.ToDictionary( t => t.Key, t => t.Value);
相关问题