1, Load the Search Box (If no any property in db, then don't display this condication)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CompleteLettingWebUI
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadMarketList();
LoadCountyList();
LoadForBeds();
LoadForMax();
LoadForMin();
}
}
#region "Load the Drop-Down List"
private void LoadMarketList()
{
using (var dc = new CompleteLettingDataContext())
{
var MarketHasProperty = dc.PropMarkets.Where(c => c.Properties.Any());
List_Market.Items.AddRange(MarketHasProperty.Where(c => c.StatusGroup == 2).Select(c => new ListItem(c.MarketName.ToString(), c.idPropMarket.ToString())).ToArray());
}
}
protected void List_Market_SelectedIndexChanged(object sender, EventArgs e)
{
using (var dc = new CompleteLettingDataContext())
{
int MarketSelectValue = Convert.ToInt32(List_Market.SelectedValue);
if (MarketSelectValue != 0)
{
var TypeHasProperty = dc.PropertyTypes.Where(c => c.Properties.Any());
int SelectClassId = dc.PropMarkets.FirstOrDefault(c => c.idPropMarket == MarketSelectValue).idPropClass;
if (SelectClassId != 1) // if the propclass is not home, then hidden the beds
{
List_Beds.Visible = false;
}
List_Type.Items.Clear();
List_Type.Items.AddRange(TypeHasProperty.Where(c => c.idPropClass == SelectClassId).Select(c => new ListItem(c.PTypeName, c.idPType.ToString())).ToArray());
}
}
}
private void LoadCountyList()
{
List_County.Items.Add(new ListItem("--Select County---","0"));
using (var dc = new CompleteLettingDataContext())
{
var countyhasProperty = dc.CountyCities.Where(c => c.Properties.Any()); // only load the county who have any properties
List_County.Items.AddRange(countyhasProperty.Where(c => c.idCountry == 1).Select(c => new ListItem(c.CountyCityName, c.idCountyCity.ToString())).ToArray());
}
}
protected void List_County_SelectedIndexChanged(object sender, EventArgs e)
{
using (var dc = new CompleteLettingDataContext())
{
int selectCountyId = Convert.ToInt32(List_County.SelectedValue);
var TheDistrictHaveProperty = dc.Districts.Where(c=>c.Properties.Any()); // only load the district who has any properties
List_District.Items.Clear();
List_District.Items.AddRange(TheDistrictHaveProperty.Where(c => c.idCountyCity == selectCountyId).Select(c => new ListItem(c.DistrictName, c.idDistrict.ToString())).ToArray());
}
}
private void LoadForBeds()
{
for(int i=1;i<=10;i++)
{
List_Beds.Items.Add(new ListItem(i.ToString(),i.ToString()));
}
}
private void LoadForMax()
{
List_Max.Items.Add(new ListItem("Price","0"));
for (int i = 100; i <= 2000; i += 100)
{
List_Max.Items.Add(new ListItem("€" + i.ToString(), i.ToString()));
}
}
private void LoadForMin()
{
List_Min.Items.Add(new ListItem("Price","0"));
for (int i = 100; i <= 2000; i += 100)
{
List_Min.Items.Add(new ListItem("€" + i.ToString(), i.ToString()));
}
List_Min.Items.Add(new ListItem("€2000+", "10000"));
}
#endregion
#region "Search function"
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
// Save the Search Conditions to the search result page
Session["MarketId"] = List_Market.SelectedValue;
Session["TypeId"] = List_Type.SelectedValue;
Session["CountyId"] = List_County.SelectedValue;
Session["DistrictId"] = List_District.SelectedValue;
Session["BedsCount"] = List_Beds.SelectedValue;
Session["MaxPrice"] = List_Max.SelectedValue;
Session["MinPrice"] = List_Min.SelectedValue;
Response.Redirect("searchResult.aspx");
}
#endregion
}
}
2, Search Function by Linq (Result display by Repeater <%# Eval("xxxx")%>)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CompleteLettingWebUI
{
public partial class searchResult : System.Web.UI.Page
{
public struct searchparameters {
public int marketId;
public int typeId;
public int countyId;
public int districtId;
public int bedcount;
public int maxPrice;
public int minPrice;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadforSearch();
lnkBackToResults.NavigateUrl = "Home.completeLet";
}
}
#region "Search Function"
private void LoadforSearch()
{
var sp = new searchparameters();
sp.marketId = Convert.ToInt32(Session["MarketId"]);
sp.typeId = Convert.ToInt32(Session["TypeId"]);
sp.countyId = Convert.ToInt32(Session["CountyId"]);
sp.districtId = Convert.ToInt32(Session["DistrictId"]);
sp.bedcount = Convert.ToInt32(Session["BedsCount"]);
sp.maxPrice = Convert.ToInt32(Session["MaxPrice"]);
sp.minPrice = Convert.ToInt32(Session["MinPrice"]);
bool anyMarket = sp.marketId.Equals(0);
bool anyType = sp.typeId.Equals(0);
bool anyCounty = sp.countyId.Equals(0);
bool anyDistrict = sp.districtId.Equals(0);
bool anyBedCount = sp.bedcount.Equals(0);
bool anyMaxPrice = sp.maxPrice.Equals(0);
bool anyMinPrice = sp.minPrice.Equals(0);
using (var dc = new CompleteLettingDataContext())
{
var SearchResult = from tb in dc.Properties
//join tb1 in dc.PropertyPictures on tb.idList equals tb1.idList into pics // for left outer join
let pic = dc.PropertyPictures.FirstOrDefault(c => c.idList == tb.idList)
let bedspaces = dc.BedroomLists.Where(c=>c.idList == tb.idList)
join tb2 in dc.PropertyTypes on tb.idPType equals tb2.idPType
join tb3 in dc.PropStatus on tb.idPropStatus equals tb3.idPropStatus
//from pic in pics
where (anyMarket || tb.idPropMarket.Equals(sp.marketId)) &&
(anyType || tb.idPType.Equals(sp.typeId)) &&
(anyCounty || tb.idCountyCity.Equals(sp.countyId)) &&
(anyDistrict || tb.idDistrict.Equals(sp.districtId)) &&
((anyMaxPrice || tb.PriceVal <= sp.maxPrice) && (anyMinPrice || tb.PriceVal >= sp.minPrice)) &&
//(anyMinPrice || tb.PriceVal >= sp.minPrice) &&
(anyBedCount || bedspaces.Sum(c=>c.Bedspaces).Equals(sp.bedcount))
select new
{
idList = tb.idList,
Picurl = "~/upload/property/" + tb.idList + "/photo/" + pic.PicPath,
DisplayAddress = tb.DisplayAddress,
Price = "€" + Convert.ToInt32(tb.PriceVal).ToString(),
RentDue = tb.RentDue != null ? "[" + tb.RentDue + "]" : "",
PropType = tb2.PTypeName,
PropertyStatus = tb3.StatusName,
Description = tb.Description.Substring(0, 200) + "......",
//LinkUrl = "DisplayProperty.aspx?idlist=" + tb.idList,
LinkUrl = "DisplayProperty.completeLet/" + tb.idList + "/"+ tb.DisplayAddress.Replace(",","_"),
BedSpace = bedspaces.Sum(c=>c.Bedspaces) != 0 ? bedspaces.Sum(c=>c.Bedspaces).ToString() + " Bedspaces" : ""
};
int resultCount = SearchResult.Distinct().Count();
if (resultCount == 0)
{
Lable_noresult.Visible = true;
Lable_noresult.Text = "Sorry, no results for your requirement.";
}
var ResultList = SearchResult.Distinct().ToList().OrderByDescending(c => c.idList);
Repeater1.DataSource = ResultList;
Repeater1.DataBind();
}
}
#endregion
}
}