簡體   English   中英

使用多個下拉列表在C#中進行高級搜索

[英]Advanced searching in c# using multple dropdownlist

嗨,我進入一個應該具有高級搜索過濾器的homefinder項目。 它應該具有3個下拉列表,用戶可以從中選擇租金金額,位置和性別類型,或者用戶也可以不選擇任何內容,因為它具有默認值“ ALL”,這會導致顯示數據庫中存儲的所有房屋,但是如果用戶從下拉列表中選擇一個或兩個值,另一個默認值為“ ALL”,或者三個都有值,則應導致它們的並集。 我嘗試使用if語句

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的房屋?

順便說一句,我已經知道如何展示房屋。 我擔心的只是如何在下拉列表中找到項目的交集。

謝謝您的任何幫助。 謝謝

取決於您進行數據訪問的方式,但是使用Linq可以使此過程更加輕松和整潔。 假設您使用的是Linq2SQL或Entity Framework,並且在數據庫中有一個名為Homes的表,那么如果您可以在名為ctx的變量中訪問EF上下文,則可以執行以下操作(警告:Air代碼,我重命名了combobox1對更有意義的東西)...

var homes = ctx.Homes;
if (cmbRentAmount.Text != "All") {
  homes = homes.Where(h => h.RentAmount == cmbRentAmount.Text);
}
// Similar for the other two combos
// ...
// Now we enumerate the query, which does the actual database access
var homesList = homes.ToList();

請注意,第一行中的homes變量是未枚舉的查詢,這意味着L2S / EF尚未實際查詢數據庫。 您經歷了三個組合,如果用戶選擇了某些內容,則修改查詢。 當枚舉homes ,它將執行查詢,並為您提供所需的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM