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 或任何其他“has”参数等于“true”时,它会返回所有记录。如果我在参数中传递“false”,则方法正常工作。

PS如果有人可以建议我更好的方法来做到这一点,我将不胜感激。是的,我知道,我的代码很幼稚。对于那个很抱歉。

谢谢。

4

1 回答 1

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();
        }

对所有其他标志进行类似的更改。

于 2012-12-30T15:39:29.970 回答