Reputation: 55
Can anyone explain why this action returns ZERO results when "0" is passed to "page" parameter:
[HttpPost]
public ActionResult SearchProperties(string id, string offerTypeID, string propertyTypeID, string page)
{
int temp = 0;
var props = from s in db.Properties
where s.Approved && s.Available
select s;
if (!String.IsNullOrEmpty(id))
{
Int32.TryParse(id, out temp);
props = from s in props
where s.PropertyType.PropertyTypeCategoryID == temp
select s;
}
if (!String.IsNullOrEmpty(offerTypeID))
{
Int32.TryParse(offerTypeID, out temp);
props = from s in props
where s.OfferTypeID == temp
select s;
}
if (!String.IsNullOrEmpty(propertyTypeID))
{
Int32.TryParse(propertyTypeID, out temp);
props = from s in props
where s.PropertyTypeID == temp
select s;
}
props = props.OrderBy(s => s.PropertyID);
int i = 0, skip = 0;
if (!String.IsNullOrEmpty(page))
{
Int32.TryParse(page, out temp);
skip = temp * 10;
}
else
{
skip = 0;
}
props = props.Skip(skip).Take(10);
var marks = (from s in props.ToList()
select s);
return Json(new { markers = marks });
}
Is it because i am re-using the temp variable to tryparse?
Please enlighten me because this piece of code doesnt throw any exception or caution but just returns zero records...
Upvotes: 0
Views: 124
Reputation: 532445
Your code is more complex that it needs to be. Simplifying it should result in a correct result or, at least, make it easier to debug. Let the framework do the conversion to int
for you. If the parameters aren't required, make them nullable.
[HttpPost]
public ActionResult SearchProperties(int? id, int? offerTypeID, int? propertyTypeID, int? page)
{
var props = from s in db.Properties
where s.Approved && s.Available
select s;
if (id.HasValue)
{
props = from s in props
where s.PropertyType.PropertyTypeCategoryID == id.Value
select s;
}
if (offerTypeID.HasValue)
{
props = from s in props
where s.OfferTypeID == offerTypeID.Value
select s;
}
if (propertyTypeID.HasValue)
{
props = from s in props
where s.PropertyTypeID == propertyTypeID.Value
select s;
}
props = props.OrderBy(s => s.PropertyID);
// use null coalescing operator to default to 0
page = page ?? 0;
int skip = page * 10;
props = props.Skip(skip).Take(10);
var marks = (from s in props.ToList()
select s);
return Json(new { markers = marks });
}
Upvotes: 3
Reputation: 3892
More than likely it relates to reusing the temp variable in TryParse, without really utilizing TryParse properly. If any of the parameters are non-null, non-empty, then no matter what it will still try to further limit your props query whether the TryParse succeeded or not.
What you should be doing is having the TryParse in a conditional statement any time you call it.
if(Int32.TryParse(propertyTypeID, out temp))
{
props = ...
}
You can also short-circuit the evaluation into the initial conditional too, since you require BOTH that it be non-null or empty AND succeed with the parse:
if(!String.IsNullOrEmpty(id) && Int32.TryParse(id, out temp))
{
props = ...
}
So if it fails in parsing the string to an integer, then it won't bother with the conditional statement.
Upvotes: 0