0

我有一个搜索字段,它使用下拉列表按部门过滤搜索结果。

我终于让它工作得很好,但现在我想做出一个 ALL 选项来否定它。下拉值等于 int。我仍在学习,所以我唯一可以解决这个问题的方法是在 where 查询中使用 if else 语句。当我这样做时,我看到了很多红色。

下拉列表中有 5 个选项。值是 1 到 5。5 = 全部。我想基本上停止按部门搜索或消除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();

感谢您的任何帮助!

4

1 回答 1

1

如何将该行替换为:

(DDLInt == 5 || t0.DivisionID == DDLInt) &&
于 2012-10-25T21:50:25.387 回答