我有一個名爲Resources的表,它有一個名爲resourceType的字段。我需要檢索每個資源類型的計數。目前我使用實體框架執行以下操作,我的asp.net mvc的內部: -如何將多個count()語句合併到一個語句中
string[] notservers = new string[] { "vmware virtual platform", "storage device", "router", "switch", "firewall" };
string[] types = new String[] { "server", "workstation" };
IT360ServerNo = entities.Resources
.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && (a.SystemInfo.ISSERVER == true) && !(notservers.Contains(a.SystemInfo.MODEL.Trim().ToLower()))).Count(),
IT360VMNo = entities.Resources
.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && (a.SystemInfo.ISSERVER == true) && a.SystemInfo.MODEL.Trim().Equals("VMware Virtual Platform", StringComparison.OrdinalIgnoreCase)).Count(),
IT360SDNo = entities.Resources
.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && a.SystemInfo.MODEL.Trim().Equals("Storage Device", StringComparison.OrdinalIgnoreCase)).Count(),
IT360SwitchNo = entities.Resources
.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && a.SystemInfo.MODEL.Trim().Equals("Switch", StringComparison.OrdinalIgnoreCase)).Count(),
IT360FirewallNo = entities.Resources
.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && a.SystemInfo.MODEL.Trim().Equals("Firewall", StringComparison.OrdinalIgnoreCase)).Count(),
IT360RouterNo = entities.Resources
.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && a.SystemInfo.MODEL.Trim().Equals("Router", StringComparison.OrdinalIgnoreCase)).Count(),
我的視圖模型類,看起來如下: -
public class SystemInformation
{
[Display(Name = "Server/s")]
public int IT360ServerNo { get; set; }
[Display(Name = "VM/s")]
public int IT360VMNo { get; set; }
[Display(Name = "SD/s")]
public int IT360SDNo { get; set; }
[Display(Name = "Switch/s")]
public int IT360SwitchNo { get; set; }
[Display(Name = "Firewall/s")]
public int IT360FirewallNo { get; set; }
[Display(Name = "Router/s")]
public int IT360RouterNo { get; set; }
}
但我的代碼將派出6個請求DB分別檢索每種類型的計數。那麼是否有辦法編寫一條語句,使得根據類型對資源表進行分組,並檢索每種類型的Count()? 感謝
編輯: - 這就是我如何填充我的模型類: -
public SystemInformation GetSystemInfo(int pagesize)
{
var d = DateTime.Today;
string[] notservers = new string[] { "vmware virtual platform", "storage device", "router", "switch", "firewall" };
string[] types = new String[] { "server", "workstation" };
var tmpCustomCount = tms.CustomAssets.Sum(a => (int?)a.Quantity);
SystemInformation s = new SystemInformation()
{
AssetCount = new AssetCount() {
CustomerCount = entities.AccountDefinitions.Count(),
RackCount = tms.TMSRacks.Count(),
ServerCount = tms.TMSServers.Count(),
VirtualMachineCount = tms.TMSVirtualMachines.Count(),
StorageDeviceCount = tms.TMSStorageDevices.Count(),
FirewallCount = tms.TMSFirewalls.Count(),
SwitchCount = tms.TMSSwitches.Count(),
RouterCount = tms.TMsRouters.Count(),
DataCenterCount = tms.DataCenters.Count(),
CustomCount = tmpCustomCount == null ? 0 : tmpCustomCount.Value
//tms.CustomAssets==null? 0 : tms.CustomAssets.Sum(a => a.Quantity)
},
AdminAudit = AllIncludingAdminAudit("", auditinfo => auditinfo.SecurityTaskType, auditinfo2 => auditinfo2.AuditAction).OrderByDescending(a => a.DateTimeStart)
.Take(pagesize).ToList(),
LatestTechnology = tms.Technologies.Where(a=> !a.IsDeleted && a.IsCompleted).OrderByDescending(a => a.TechnologyID).Take(pagesize).ToList(),
IT360ServerNo = entities.Resources
.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && (a.SystemInfo.ISSERVER == true) && !(notservers.Contains(a.SystemInfo.MODEL.Trim().ToLower()))).Count(),
IT360VMNo = entities.Resources
.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && (a.SystemInfo.ISSERVER == true) && a.SystemInfo.MODEL.Trim().Equals("VMware Virtual Platform", StringComparison.OrdinalIgnoreCase)).Count(),
IT360SDNo = entities.Resources
.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && a.SystemInfo.MODEL.Trim().Equals("Storage Device", StringComparison.OrdinalIgnoreCase)).Count(),
IT360SwitchNo = entities.Resources
.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && a.SystemInfo.MODEL.Trim().Equals("Switch", StringComparison.OrdinalIgnoreCase)).Count(),
IT360FirewallNo = entities.Resources
.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && a.SystemInfo.MODEL.Trim().Equals("Firewall", StringComparison.OrdinalIgnoreCase)).Count(),
IT360RouterNo = entities.Resources
.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && a.SystemInfo.MODEL.Trim().Equals("Router", StringComparison.OrdinalIgnoreCase)).Count(),
DeleteNo = tms.TechnologyAudits.Where(a => (EntityFunctions.TruncateTime(a.DateTimeEnd) == d && a.AuditAction.Name.ToLower() == "delete" && a.TechnologyID != null)).Count(),//TechnologyId != null so that only assets that have tags will be included in the count
CreateNo = tms.TechnologyAudits.Where(a => (EntityFunctions.TruncateTime(a.DateTimeEnd) == d && a.AuditAction.Name.ToLower() == "add" && a.TechnologyID != null)).Count(),
EditNo = tms.TechnologyAudits.Where(a => (EntityFunctions.TruncateTime(a.DateTimeEnd) == d && a.AuditAction.Name.ToLower() == "Edit" && a.TechnologyID != null)).Count(),
OtherNo = tms.TechnologyAudits.Where(a => (EntityFunctions.TruncateTime(a.DateTimeEnd) == d
&&
!((a.AuditAction.Name.ToLower() == "delete" && a.TechnologyID != null)
|| (a.AuditAction.Name.ToLower() == "add" && a.TechnologyID != null)
|| (a.AuditAction.Name.ToLower() == "edit" && a.TechnologyID != null)))).Count(),
};
return s;
是否實體'SystemInfo'有屬性'Resources'辦呢?獲取'SystemInfo's並執行'SystemInfo.Resources.Count()'更容易。 –
是的,這種關係是一對一的。我需要資源中的AssetTag字段,而我需要系統信息中的IsServer。所以無論哪種方式都是一樣的。我無法得到你的意思? –