【问题标题】:How to get every Value from DB if nothing is selected in DropDown?如果在下拉菜单中未选择任何内容,如何从数据库中获取每个值?
【发布时间】:2014-06-23 11:06:21
【问题描述】:

我有一个新问题,或者我只是有点卡住了我的头。

我有几个 DropDownLists。在那里你可以选择一个值或什么都没有。两者对于选择值后的 LINQ 语句中的 where 子句都很重要。所以我需要的是,如果你选择了一个值,它应该说(例如):

var result = from c in context.table
             where c.name == dropdown.selectedvalue
             select c.id;

这是更容易的部分。所以我的问题是,如果您在下拉列表中选择了 null/empty 字段,它应该给我来自数据库的每个结果。与上面相同的语句类似,但没有 where 子句。

我最大的问题是,我有 4 个不同的 DropDownLists,每个 List 都是 where 子句的一个元素。我的 LINQ 语句长约 30 行,我不想为每种情况复制语句 5 次。

var finishedReports = (from r in context.response
                       from c in context.maxCTs
                       from t in context.ticket
                       join tp in context.priorities
                           on t.ticket_priority_id equals tp.id
                       join ts in context.states
                           on t.ticket_state_id equals ts.id
                       from a in context.article
                       where (from ti in context.ticket
                              where ti.tn == r.tn
                              select new { ti.id }).Contains(new { id = a.ticket_id })
                       where r.change_time >= startdat &&
                           r.change_time <= enddat &&
                           r.tn == c.tn &&
                           t.tn == r.tn
                       //where t.queue_id == queueID &&
                       //    tp.id == priority &&
                       //    ts.name.Contains(status)
                       orderby r.tn
                       select new
                       {
                           r.tn,
                           r.title,
                           a.id,
                           a.a_subject,
                           a.article_type_id,
                           a.a_from,
                           tp.id,
                           ts.name,
                           r.change_time,
                           r.start_date,
                           r.create_date,
                           c.finish_date
                       }).Distinct();

当从 DropDownList 中选择的值为空或为空时,我如何在语句中写入它应该从数据库中获取每个数据? (上面代码中的注释行是比较下拉列表中的值的 where 子句)

感谢您的每一个帮助,如果我的问题令人困惑,我会改变它!

【问题讨论】:

    标签: c# asp.net sql linq


    【解决方案1】:

    有一个这样的 lambda,如果下拉列表有值,则只会生成 SQL

    where dropdown.value == "" || db.myfield == dropdown.value
    

    【讨论】:

    • 但它也应该在下拉列表中未选择任何内容(= 所有内容)时生成。我也可以在列表中添加一个项目,上面写着“全部”,但是我必须在语句中写入 - if (selectedvalue == all) take every data from DB - 但是不可能将 if 写入 where 子句(?!)。我必须更改hole linq 查询,这就是我不想做的(我认为有更简单的方法?!)
    • 正确!在 lambda 中写“If”的方法是使用 ||和&&
    • 不是真的 - ||和 && 完全不同于 If(case) then else?!?!而且我不明白您的where dropdown.value == "" 的含义-我需要它,例如if(dropdown.value == null) { do NOT add clause to where statement } else if(dropdown.value == 1) { add a special clause to where statement } 等等。
    • 好的,使用布尔逻辑它看起来像 ((dropdown.value == 1) && (add special Clause)) || (dropdown.value == 2) && (其他一些特殊情况)),不需要指定null case,只需要在where中指定所有实际需要生成的东西即可。
    • 所以你的意思是我应该在 where 子句中添加 EVERY CASE?那将是一个有趣的长 where 子句。最后,这取决于登录用户我有多少 CheckBoxList 中的案例。我只是认为我的问题是错误的。我会过度劳累,也许这样更容易理解我需要什么。
    猜你喜欢
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多