只是一个简单的类,我不确定我需要返回什么列表类型,字符串不起作用,也不是空白,对于知道的人来说应该很简单。
感谢您的帮助
public static List<> lstVMStats()
{
ITAPP.Models.DBEntities db = new ITAPP.Models.DBEntities();
var varESxis = from d in db.tblVirtualServers
group d by d.ESXiID into g
orderby g.Count() descending, g.Key
select new {
ESXi = g.Key,
Count = g.Count()
};
return varESxis.ToList();
}
答案 0 :(得分:4)
如果列表包含匿名对象,则需要从方法返回List<object>
。但是它不会非常有用,因为调用它的其他方法无法访问匿名类型中定义的属性。您应该为结果创建自定义类型。
class VirtualServerInfo
{
public string ESXi { get; set; }
public int Count { get; set; }
}
public static List<VirtualServerInfo> lstVMStats()
{
ITAPP.Models.DBEntities db = new ITAPP.Models.DBEntities();
var varESxis = from d in db.tblVirtualServers
group d by d.ESXiID into g
orderby g.Count() descending, g.Key
select new VirtualServerInfo{
ESXi = g.Key,
Count = g.Count()
};
return varESxis.ToList();
}
答案 1 :(得分:0)
您可以改为返回词典:
public static Dictionary<string, int> lstVMStats()
{
ITAPP.Models.DBEntities db = new ITAPP.Models.DBEntities();
var varESxis = from d in db.tblVirtualServers
group d by d.ESXiID into g
orderby g.Count() descending, g.Key
select new {
ESXi = g.Key,
Count = g.Count()
};
return varESxis.ToDictionary(x=>x.Key, y=>y.Value);
}