I have a search field that uses a Drop down list to Filter search results by division.
I finally got it working great but now I want to make an ALL option to negate this. The drop down values are equal to an int. I am still learning so the only way I can bend my mind around this would be a if else statement in the where query. When i did that i saw a lot of red.
There are 5 options on the Drop down list. The values are 1 through 5. 5 = all. I want to basically stop searching by division or eliminate the t0.DivisionID == DDLInt &&
string DDL = DDLAddDivision.SelectedValue;
int DDLInt;
if (int.TryParse(DDL, out DDLInt))
{
int searchID;
if (!int.TryParse(txtSearch.Text.Trim(), out searchID))
searchID = -1; // set to an invalid ID
ItemContext db = new ItemContext();
var q = (from t0 in db.Item
join t1 in db.Categories on t0.CategoryID equals t1.CategoryID
join t2 in db.Divisions on t0.DivisionID equals t2.DivisionID
where
t0.DivisionID == DDLInt &&
//Contains
(
t0.ItemName.Contains(txtSearch.Text.Trim()) ||
t0.Email.Contains(txtSearch.Text.Trim()) ||
t0.Description.Contains(txtSearch.Text.Trim()) ||
t0.Phone.Contains(txtSearch.Text.Trim()) ||
t0.ItemID.Equals(searchID)
)
group t0 by new
{
t0.ItemID,
t0.ItemName,
t0.Description,
t0.Phone,
t0.Email,
t0.DivisionID,
t0.CategoryID,
t1.CategoryName,
t2.DivisionName
} into i
select new
{
i.Key.ItemID,
i.Key.ItemName,
i.Key.Description,
i.Key.Email,
i.Key.Phone,
i.Key.CategoryName,
i.Key.DivisionName,
i.Key.CategoryID,
i.Key.DivisionID
});
if (q.Any() == false)
{
ResultsLabelId.Visible = true;
}
else
{
ResultsLabelId.Visible = false;
}
//bind and return
lv.DataSource = q.ToList();
lv.DataBind();
Thank you for any and all help!
How about replacing that line with: