【问题标题】:Linq Where statements in If conditions VB.netIf条件VB.net中的Linq Where语句
【发布时间】:2018-11-22 01:45:12
【问题描述】:

我被困在 LINQ 中,我必须根据复选框的值指定 where 子句。我尝试寻找答案,但其中大部分是 C# 语言,我不太擅长理解它的语法。

      Dim recs = From exp In db.exprevs
               Where exp.school_id = currentschool.school_id And exp.type = 1


    If chbid.IsChecked = True Then
        ''here I want to tell the query to filter where ID is something
    End If

    If chbname.IsChecked = True Then
        ''here I want to filter where name is something
    End If

    dgsearchresult.ItemsSource = recs

如果有人指导我如何进一步过滤查询,那就太好了。

【问题讨论】:

  • 这里没有足够的信息来提供帮助。表达式的模型是什么? chbid 和 chbname 与该模型有何关系?检查 chbname 是否应该覆盖 chbid 的查询?
  • AND 将多个条件组合在一起就像在查询中链接 Where 调用一样简单,例如recs = recs.Where(Function(exp) exp.ID = someValue)。您可以根据需要多次执行此操作。如果您想对条件进行 OR 操作,那就没那么简单了。
  • @jmcilhinney 我能猜到的最好的结果是他正在尝试查询 Where exp.ID = (a textbox?) if chbid 和 query Where exp.Name = (a textbox?) if chbname .因此,最初的查询是一种浪费,但没有足够的信息。
  • @jmcilhinney 如果您将此作为答案发布,我会接受它。它起作用了,我错过了 function(exp) 部分。非常感谢。
  • @NathanChampion 是的,这正是我想要做的。

标签: .net wpf vb.net linq-to-sql


【解决方案1】:

根据jmcilhinney 对帖子的评论,我能够解决我的问题。为了过滤 if 语句中的某些内容,我必须在我的案例中编写这段简短的代码。

    recs = recs.Where(Function(exp) exp.ID = txtID.text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    相关资源
    最近更新 更多