我一直在尝试编写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”param等于“true”时,它会返回所有记录。如果我在params中传递“false”,方法正常工作。
P.S。如果有人能建议我这样做的更好方法,我将不胜感激。 是的,我知道,我的代码是幼稚的。对不起。
感谢。
答案 0 :(得分: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();
}
对所有其他标志进行类似的更改。