【问题标题】:Linq query with toDictionary使用 toDictionary 进行 Linq 查询
【发布时间】:2015-04-16 06:58:43
【问题描述】:

我有以下 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();

我需要将 Dictionary 与 int 和 string 类型一起使用,而不是将结果转换为 StringIntType。

【问题讨论】:

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


    【解决方案1】:

    你可以试试这样的:

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

    【讨论】:

      【解决方案2】:

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

      基本上,使用 ToDictionary 代替 ToList

      .ToDictionary( t => t.Key, t => t.Value);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-06
        • 1970-01-01
        • 1970-01-01
        • 2017-04-10
        • 2013-11-22
        • 1970-01-01
        相关资源
        最近更新 更多