2014-01-12 130 views
0

我有一個名爲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; 
+0

是否實體'SystemInfo'有屬性'Resources'辦呢?獲取'SystemInfo's並執行'SystemInfo.Resources.Count()'更容易。 –

+0

是的,這種關係是一對一的。我需要資源中的AssetTag字段,而我需要系統信息中的IsServer。所以無論哪種方式都是一樣的。我無法得到你的意思? –

回答

0
var assets = entities.Resources.Where(a => String.IsNullOrEmpty(a.ASSETTAG)).ToList() 

然後使用assets.Where ......這將導致一個查詢。那麼你的應用程序將整理並計數,而不是數據庫的...

+0

但在你的方法中,我將檢索從數據庫到服務器的所有記錄及其數據。雖然我只需要計數,你的方法會導致網絡利用率不好... –

1

您可以與let聲明

var systemInformation = (from r in entities.Resources 
         let serverNos = r.Where(a => String.IsNullOrEmpty(a.ASSETTAG) && (a.SystemInfo.ISSERVER == true) && !(notservers.Contains(a.SystemInfo.MODEL.Trim().ToLower()))) 
         let vmnos = r.[...] 
         select new SystemInformation 
            { 
             IT360ServerNo = serverNos.Count(), 
             IT360VMNo = vmnos.Count(), 
             [...] 
            }).First(); 
+0

感謝您的答覆,我編輯了我的原始問題,包括我如何填充模型類的完整代碼。因爲你的評價在我的情況下不起作用。對不起,從一開始就不包括完整的代碼。 –

+0

我也認爲你的查詢語法是錯誤的,因爲我不敢打電話給r.Where後的Where! –

相關問題