2012-12-30 51 views
1

我一直在嘗試編寫一個RESTful API方法。但它沒有按預期工作。搜索方法未按預期工作

代碼可以說明問題。完整的方法如下:

public CustomResult Query(
     int? count, 
     string os = null, 
     string manufacturer = null, 
     string has_camera = null, 
     string has_gprs = null, 
     string has_wifi = null, 
     string has_edge = null, 
     string has_bluetooth = null, 
     string has_gpu = null, 
     string has_gps = null, 
     string has_radio = null 
     ) 
    { 
     TimeSpan T = DateTime.Now.TimeOfDay; 
     Result result = new Result(); 
     List<Mobile> MbList = getMobileList(); 

     if (os != null) 
     { 
      MbList = (from M in MbList 
         where (M.Feature.OS.ToLower().IndexOf(os.ToLower()) >= 0) 
         select M).ToList(); 
     } 
     if (manufacturer != null) 
     { 
      MbList = (from M in MbList 
         where (M.Manufacturer.ToLower() == manufacturer.ToLower()) 
         select M).ToList(); 
     } 

     #region Camera Check 

     if (has_camera != null) 
     { 
      if (has_camera == "true") 
      { 
       MbList = (from M in MbList 
          where (M.Camera.Primary.ToLower() != "n/a" || M.Camera.Primary.ToLower() != "no") 
          select M).ToList(); 
      } 
      else 
      { 
       MbList = (from M in MbList 
          where (M.Camera.Primary.ToLower() == "n/a" || M.Camera.Primary.ToLower() == "no") 
          select M).ToList(); 
      } 
     } 

     #endregion 
     #region GPRS Check 
     if (has_gprs != null) 
     { 
      if (has_gprs == "true") 
      { 
       MbList = (from M in MbList 
          where (M.Data.GPRS.ToLower() != "n/a" || M.Data.GPRS.ToLower() != "no") 
          select M).ToList(); 
      } 
      else 
      { 
       MbList = (from M in MbList 
          where (M.Data.GPRS.ToLower() == "n/a" || M.Data.GPRS.ToLower() != "no") 
          select M).ToList(); 
      } 
     } 
     #endregion 
     #region Wifi Check 

     if (has_wifi != null) 
     { 
      if (has_wifi == "true") 
      { 
       MbList = (from M in MbList 
          where (M.Data.WAN.ToLower() != "n/a" || M.Data.WAN.ToLower() != "no") 
          select M).ToList(); 
      } 
      else 
      { 
       MbList = (from M in MbList 
          where (M.Data.WAN.ToLower() == "n/a" || M.Data.WAN.ToLower() == "no") 
          select M).ToList(); 
      } 
     } 

     #endregion 
     #region GPU Check 

     if (has_gpu != null) 
     { 
      if (has_gpu.ToLower().CompareTo("true") > 0) 
      { 
       MbList = (from M in MbList 
          where (M.Feature.GPU.ToLower() != "n/a" || M.Feature.GPU.ToLower() != "no") 
          select M).ToList(); 
      } 
      else 
      { 
       MbList = (from M in MbList 
          where (M.Feature.GPU.ToLower() == "n/a" || M.Feature.GPU.ToLower() == "no") 
          select M).ToList(); 
      } 
     } 

     #endregion 
     #region EDGE Check 
     if (has_edge != null) 
     { 
      if (has_edge == "true") 
      { 
       MbList = (from M in MbList 
          where (M.Data.EDGE.ToLower() != "n/a" || M.Data.EDGE.ToLower() != "no") 
          select M).ToList(); 
      } 
      else 
      { 
       MbList = (from M in MbList 
          where (M.Data.EDGE.ToLower() == "n/a" || M.Data.EDGE.ToLower() == "no") 
          select M).ToList(); 
      } 
     } 

     #endregion 
     #region Bluetooth Check 
     if (has_bluetooth != null) 
     { 
      if (has_bluetooth == "true") 
      { 
       MbList = (from M in MbList 
          where (M.Data.Bluetooth.ToLower() != "n/a" || M.Data.Bluetooth.ToLower() != "no") 
          select M).ToList(); 
      } 
      else 
      { 
       MbList = (from M in MbList 
          where (M.Data.Bluetooth.ToLower() == "n/a" || M.Data.Bluetooth.ToLower() == "no") 
          select M).ToList(); 
      } 
     } 
     #endregion 
     #region GPS Check 
     if (has_gps != null) 
     { 
      if (has_gps == "true") 
      { 
       MbList = (from M in MbList 
          where (M.Feature.GPS.ToLower() != "n/a" || M.Feature.GPS.ToLower() != "no") 
          select M).ToList(); 
      } 
      else 
      { 
       MbList = (from M in MbList 
          where (M.Feature.GPS.ToLower() == "n/a" || M.Feature.GPS.ToLower() == "no") 
          select M).ToList(); 
      } 
     } 
     #endregion 
     #region Radio Check 
     if (has_radio != null) 
     { 
      if (has_radio == "true") 
      { 
       MbList = (from M in MbList 
          where (M.Feature.Radio.ToLower() != "n/a" || M.Feature.Radio.ToLower() != "no") 
          select M).ToList(); 
      } 
      else 
      { 
       MbList = (from M in MbList 
          where (M.Feature.Radio.ToLower() == "n/a" || M.Feature.Radio.ToLower() == "no") 
          select M).ToList(); 
      } 
     } 
     #endregion 

     if (count.HasValue) 
     { 
      MbList = MbList.Take(count.Value).ToList<Mobile>(); 
     } 
     result.Count = MbList.Count; 
     result.Data = MbList; 
     result.TimeElapsed = getElapsedTiming(T); 
     return new CustomResult() { R = result }; 
    } 

的問題是,當我通過has_camera或任何其他「具有」參數等於「真」,則返回我的所有記錄。如果我在params中傳遞「false」,方法正常工作。

P.S.如果任何人都可以建議我更好的方法來做到這一點,我會很感激。 是的,我知道,我的代碼是幼稚的。對於那個很抱歉。

謝謝。

+0

這可能會讓事情更容易爲您的條件寫出真值表。這就清楚地表明你想要成爲真實的人,以及你想成爲什麼樣的人。 – ChrisF

回答

1

對於真實的情況,您正在檢查_ != "n/a" || _ !="no'。這並不排除"no"。爲此,您需要&&而不是||

更改以下爲has_camera標誌。

 if (has_camera == "true") 
     { 
      MbList = (from M in MbList 
         where (M.Camera.Primary.ToLower() != "n/a" || M.Camera.Primary.ToLower() != "no") 
         select M).ToList(); 
     } 

 if (has_camera == "true") 
     { 
      MbList = (from M in MbList 
         where (M.Camera.Primary.ToLower() == "n/a" && M.Camera.Primary.ToLower() == "no") 
         select M).ToList(); 
     } 

做所有其他標誌相似的變化。

+0

我試過(!(M.Camera.Primary.ToLower()==「n/a」|| M.Camera.Primary.ToLower()==「no」)),它工作。和(M.Camera.Primary.ToLower()!=「n/a」&& M.Camera.Primary.ToLower()!=「no」)也在起作用。萬分感謝。公認。 – Umayr

+0

是的,'a && b'與'!a ||!b'相同 – Tilak