我有一个“服务器组”对象列表,其中包含“受监控服务器”,后者又包含“服务器脚本状态”。我有这些脚本状态的回购。目前我正在尝试查找受监控服务器数量的计数。我现在已经搞乱了几个小时,似乎无法得到它。我不断收回每个受监控服务器中脚本数量的计数,但我无法获得服务器组下的受监控服务器数量。我是LINQ的新手,很抱歉,如果这是愚蠢的话。谢谢大家!
//This is List of ScriptStatuses
var scriptsInCategory = scriptRepository.ScriptStatuses
.Where(p => p.MonitoredServer.ServerGroup.ServerCategory.Name == category);
var groupsList = new List<NodeSummaryListViewModel>();
foreach (var s in scriptsInCategory.GroupBy(p=>p.MonitoredServer.ServerGroup.Name))
{
NodeSummaryListViewModel viewModel = new NodeSummaryListViewModel
{
Name = s.First().MonitoredServer.ServerGroup.Name,
Description = s.First().MonitoredServer.ServerGroup.Description,
//I want NumServers to be the count of Monitored Servers under the server group
NumServers = (from z in scriptsInCategory
group z by z.MonitoredServer.ServerGroup into g
select new { ServerGroup = g.Key, MonSvrCount = g.Count() }).Count(),
NumOk = PopulateGroupCellData(CellState.Ok, s),
NumAttention = PopulateGroupCellData(CellState.Attention,s),
NumCritical = PopulateGroupCellData(CellState.Critical, s),
NumUnavailable = PopulateGroupCellData(CellState.Unavailable, s),
NumOffline = PopulateGroupCellData(CellState.Offline, s)
};
groupsList.Add(viewModel);
}
答案 0 :(得分:1)
我想只是
NumServers = s.Count(),
应该有用。
答案 1 :(得分:0)
我不确定我是否理解这个问题,但这对你有意义吗?
from g in groups select new{ g, monitoredServers = g.Servers.Count(s=>s.Scripts > 0)}
因此,您将获得一个组列表,其中包含该组中正在监视的服务器数量。