Reputation: 97
I have been trying to write a RESTful API method. But it is not working as expected.
Code speaks for itself. Complete Method is as follows:
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 };
}
The problem is, when I pass has_camera or any other "has" param equals to "true", it returns me all records. Method is working properly if I pass "false" in params.
P.S. If anyone can suggest me a better way to do this, I'll be grateful. And yes I know, my code is childish. Sorry about that.
Thanks.
Upvotes: 1
Views: 70
Reputation: 30738
For true condition, you are checking _ != "n/a" || _ !="no'
. This does not exclude "no"
. For that you need &&
instead of ||
.
Change following for has_camera
flag.
if (has_camera == "true")
{
MbList = (from M in MbList
where (M.Camera.Primary.ToLower() != "n/a" || M.Camera.Primary.ToLower() != "no")
select M).ToList();
}
To
if (has_camera == "true")
{
MbList = (from M in MbList
where (M.Camera.Primary.ToLower() == "n/a" && M.Camera.Primary.ToLower() == "no")
select M).ToList();
}
Do similar change for all other flags.
Upvotes: 1