使用多个下拉列表在C#中进行高级搜索 [英] Advanced search in C# using multiple dropdownlist

查看:179
本文介绍了使用多个下拉列表在C#中进行高级搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在进入一个应该拥有高级搜索过滤器的homefinder项目。它应该有3个下拉列表,用户可以从中选择租金金额,位置和性别类型,或者用户也可以不选择任何因为它具有默认值ALL,这导致显示数据库中存储的所有房屋但是如果用户从下拉列表中选择一个或两个值,其他默认值为ALL,或者三者都有值,它应该导致它们的并集。



我尝试了什么:



我尝试使用if语句

Hi I am into a homefinder project that should have an advanced search filter. It should have 3 dropdownlist from where the user can select rent amount, location, and gender type or the user can also Not select any bcause it has default value "ALL" which leads to showing all the houses stored n the database but if the user selects one or two values from the dropdownlist with the other default value as "ALL", or all the three has values, it should lead to the union of them.

What I have tried:

I tried using if statements

if (combobox1.text == "ALL" && combobox2.text == "ALL" && combobox3.text == "ALL")
{
 // shows all the homes
}
else if ( combobox1.text != "ALL" && combobox2.text == "ALL" && combobox3.text == "ALL")
{
 // say combobox1 is rent amount, shows all the homes having that rent amount
}

else if (combobox1.text == "ALL" && combobox2.text != "ALL" && combobox3.text == "ALL")
{
   // say combobox2 is gender type shows all the homes having that        gender category
   if (combox2.text == "Female")
     // all homes w "female"
   else
     // all homes w male
}

else if ( combobox1.text == "ALL" && combobox2.text == "ALL" && combobox3.text != "ALL")
{
// say combobox3 is location, shows all homes in that location
}

else if ( combobox1.text != "ALL" && combobox2.text != "ALL" && combobox3.text != "ALL")
{
}
else if ( combobox1.text != "ALL" && combobox2.text != "ALL" && combobox3.text == "ALL")
{
}



等等,这是我到目前为止所考虑的代码:l我怎么能做他们的交集。如果我选择500以下的租金和第一街的位置,我怎样才能找到位于第一街的租金为500的房屋?





感谢您提供的任何帮助。谢谢。


and so on, this is the code I have thought so far :l how can I make the intersection of them. Like if I choose 500 under rentamount and 1st Street under location, how can I find the homes with 500 as rent amount that is located in the 1st Street?


Any help from you is appreciated. Thank you.

推荐答案

假设你有一个SqlCommand对象我将调用 sqlCommand 和你的3个名字很糟糕的组合框是按顺序c1 - 租,c2 - 性别,c3 - 位置(我用的是速记,因为我无法在我的沙盒表格上创建3个组合框)



首先你需要知道哪个(如果有的话)的组合框被设置为默认的ALL......

Assuming that you have a SqlCommand object which I will call sqlCommand and your 3 badly named comboBoxes are in order c1 - rent, c2 - gender, c3 - location (I'm using the shorthand because I could not be bothered to create 3 comboBoxes on my sandbox form)

Firstly you need to know which, if any, of the comboBoxes are set to the default of "ALL"...
var b1 = (c1 == "ALL");
var b2 = (c2 == "ALL");
var b3 = (c3 == "ALL");

这些布尔值有助于控制将要放入WHERE子句的内容。因为我们要动态地添加到where子句并且因为字符串是不可变的,所以使用stringBuilder来生成查询...

These Booleans help to control what you are going to put into your WHERE clause. Because we are going to "add" to the where clause dynamically and because strings are immutable, use a stringBuilder to generate the query...

var whereQuery = new StringBuilder(" WHERE ");



然后根据布尔值将这些部分添加到WHERE子句中......


Then add the sections into the WHERE clause based on the Boolean values...

if (b1)
{
    whereQuery.Append("rentColum = @rent");
    sqlCommand.Parameters.AddWithValue("@rent", c1);
}
if (b2)
{
    whereQuery.Append((b1) ? " AND " : "");
    whereQuery.Append("genderColumn = @gender");
    sqlCommand.Parameters.AddWithValue("@gender", c2);
}
if (b3)
{
    whereQuery.Append((b1 || b2) ? " AND " : "");
    whereQuery.Append("locationColumn = @location");
    sqlCommand.Parameters.AddWithValue("@location", c3);
}



请注意,我正在构建参数化查询。另请注意,如果在c2,c3,(等)之前添加了过滤器,则仅添加AND的方式



当你完成建筑的地方然后将它添加到SELECT查询


Note that I'm building a parameterised query. Also note the way that " AND " is only added if there has been a filter added prior to c2, c3, (etc)

When you have finished building your where clause then add it to your SELECT query

selectQuery += whereQuery.ToString();



请注意,因为您的问题中缺少信息这个答案完全没有经过考验。可能存在轻微的输入错误。


Please note, because of the lack of information in your question this answer is completely untested. There may be minor typing errors.


这篇关于使用多个下拉列表在C#中进行高级搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆