我已经为我的WebMatrix属性网站构建了一个完整的搜索页面。总而言之,它会检查查询字符串,如果存在任何已定义的值,它会将其添加到列表中,以便它可以遍历它们。
问题,正如您将看到的,如果它当前将所有内容作为字符串添加到列表中。当我需要将值用作INT(PropertyType和NumBedrooms应该是int)时,这会导致问题。有没有办法在这种情况下可以使用字符串和int?我可以将整数和字符串添加到同一个列表中吗?
这是代码:
var db = Database.Open("StayInFlorida");
var proptype = db.Query("SELECT * FROM Property_Type");
IEnumerable<dynamic> queryResults;
//Search Variables
string searchTerm = "";
string resortID = "";
string propertyType = "";
string numBedrooms = "";
List<string> argList = new List<string>();
//Paging Variables
var pageSize = 6;
var totalPages = 0;
var count = 0;
var page = UrlData[0].IsInt() ? UrlData[0].AsInt() : 1;
var offset = (page -1) * pageSize;
//Request querystrings from URL
if (!String.IsNullOrWhiteSpace(Request.QueryString["searchTerm"]))
{
searchTerm = Request.QueryString["searchTerm"];
}
if (!String.IsNullOrWhiteSpace(Request.QueryString["resortID"]))
{
resortID = Request.QueryString["resortID"];
}
if (!String.IsNullOrWhiteSpace(Request.QueryString["propertyType"]))
{
propertyType = Request.QueryString["propertyType"];
}
if (!String.IsNullOrWhiteSpace(Request.QueryString["numBedrooms"]))
{
numBedrooms = Request.QueryString["numBedrooms"];
}
int numOfArguments = 0;
string selectQueryString = "SELECT * FROM Property_Info ";
if (searchTerm != "")
{
argList.Add(searchTerm);
selectQueryString += "WHERE FullDescription LIKE '%' + @0 + '%' OR BriefDescription LIKE '%' + @0 + '%' ";
numOfArguments++; //increment numOfArguments by 1
}
if (resortID != "")
{
argList.Add(resortID);
if (numOfArguments == 0)
{
selectQueryString += "WHERE ResortID = @0 ";
}
else
{
selectQueryString += "AND ResortID = @" + numOfArguments + " ";
}
numOfArguments++;
}
if (propertyType != "")
{
argList.Add(propertyType);
if (numOfArguments == 0)
{
selectQueryString += "WHERE PropertyTypeID = @0 ";
}
else
{
selectQueryString += "AND PropertyTypeID = @" + numOfArguments + " ";
}
numOfArguments++;
}
if (numBedrooms != "")
{
argList.Add(numBedrooms);
if (numOfArguments == 0)
{
selectQueryString += "WHERE NumBedrooms = @0 ";
}
else
{
selectQueryString += "AND NumBedrooms = @" + numOfArguments + " ";
}
numOfArguments++;
}
selectQueryString += "ORDER BY CreatedDate DESC";
string[] argArray = argList.ToArray();
queryResults = db.Query(selectQueryString, argArray);
count = queryResults.Count();
totalPages = count/pageSize;
if(count % pageSize > 0){
totalPages += 1;
}
Polynomial建议后编辑的代码:
@{
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Search Page";
var db = Database.Open("StayInFlorida");
var proptype = db.Query("SELECT * FROM Property_Type");
//Paging Variables
var pageSize = 6;
var totalPages = 0;
var count = 0;
var page = UrlData[0].IsInt() ? UrlData[0].AsInt() : 1;
var offset = (page -1) * pageSize;
IEnumerable<dynamic> queryResults;
//Request querystrings from URL
string selectQueryString = "SELECT * FROM Property_Info WHERE 0 = 0 AND NumBedrooms = @0 AND NumBathrooms = @1 ";
selectQueryString += "ORDER BY CreatedDate DESC";
queryResults = db.Query(selectQueryString, Request.QueryString["NumBedrooms"], Request.QueryString["NumBathrooms"]);
count = queryResults.Count();
totalPages = count/pageSize;
if(count % pageSize > 0){
totalPages += 1;
}
}
答案 0 :(得分:1)
嗯,有很多方法可以解决这个问题,我认为最简单的一个不涉及不使用命名序数参数化的方法就是对每个搜索条件使用固定的序数,而不是使用你没有使用的那些需要在SQL语句中。你的代码很长,所以我真的不想在这里尝试重写整个事情,但基本上如果你的db.Query()
调用将所有潜在的参数作为参数传递给你:
queryResults = db.Query(selectQueryString, Request.QueryString["searchTerm"], Request.QueryString["resortId"]).AsInt(), Request.QueryString["propertyType"].AsInt() /* etc etc */);
然后你的序数总是相同的数字。 searchTerm
始终为@0
,resortId
始终为@1
等等。您根本不需要数组,只需要为此构建适当的SQL语句用户指定的内容,例如一些符合上述代码的WHERE
子句可以是:
"WHERE ResortID = @1"
"WHERE PropertyType = @2"
"WHERE ResortID = @1 AND PropertyType = @2"
"WHERE Name = @0"
"WHERE Name = @0 AND ResortID = @1 AND PropertyType = @2"
如果子句仅使用一个或两个参数,则其他参数只会被忽略,希望一切都按预期工作。
编辑以显示代码:
// Basic beginning of the select statement
string selectQueryString = "SELECT * FROM Property_Info WHERE 0 = 0 ";
// Check to see if the user specified bedrooms, if they did add to the select statement
if (Request.QueryString["NumBedrooms"].AsInt() > 0)
{
selectQueryString += "AND NumBedrooms = @0 ";
}
// Same for the number of bathrooms
if (Request.QueryString["NumBathrooms"].AsInt() > 0)
{
selectQueryString += "AND NumBathrooms = @1 ";
}
// ** Add more criteria to your select list here by checking for valid values and concatenating your string **
// Finally add the order by on the end
selectQueryString += "ORDER BY CreatedDate DESC";
// Execute the statement
queryResults = db.Query(selectQueryString, Request.QueryString["NumBedrooms"].AsInt(), Request.QueryString["NumBathrooms"].AsInt());
这应该为您提供一个非常灵活和可扩展的搜索条件系统,该系统满足零,一个或多个标准。