我有一个数据库,每'n'秒插入一次CPU使用率,我的任务是从数据库中获取最新的CPU条目。我想显示数据库tat中属于特定服务器的最新CPU条目,并将结果保存到通用列表中。

我尝试过的

    string match = "ServerX"

    List<UsageCPU> cpus = (from a in db.UsageCPUs
                          where a.ServerID.Contains(match)
                          group a by a.ServerID into b
                          orderby b.Key
                          select new { ServerID = b.Key,
                          Usage = b.FirstOrDefault() }).ToList();

我想遍历结果cpus以执行其他步骤。

但我收到以下错误



编辑

更新
来自Satpal的以下查询对我有用..但是使用了var
   var cpus = (from a in db.UsageCPUs
                    where a.ServerID.Contains(match)
                    group a by a.ServerID into b
                    orderby b.Key
                    select new
                    {
                        ServerID = b.Key,
                        Usage = b.FirstOrDefault().Usage
                    }).ToList();

        foreach (var item in cpus)
        {
           if(bla bla..)
        }

请帮助!

最佳答案

采用

List<UsageCPU> cpus = (from a in db.UsageCPUs
                      where a.ServerID.Contains(match)
                      group a by a.ServerID into b
                      orderby b.Key
                      select new UsageCPU  //Added UsageCPU
                      {
                          ServerID = b.Key,
                          Usage = b.FirstOrDefault().Usage
                      }).ToList();

根据错误更新


List<UsageCPU> cpus = (from a in db.UsageCPUs
                      where a.ServerID.Contains(match)
                      group a by a.ServerID into b
                      orderby b.Key
                      select new
                      {
                          ServerID = b.Key,
                          Usage = b.FirstOrDefault().Usage
                      })
                    .AsEnumerable()
                    .Select(x => new UsageCPU
                    {
                       ServerID ,
                       Usage
                    }).ToList();

关于c# - 如何将通用列表<匿名类型>转换为通用列表<Classname>?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17275871/

10-11 14:59