【问题标题】:LINQ filtering based on preview filter基于预览过滤器的 LINQ 过滤
【发布时间】:2013-08-25 06:34:07
【问题描述】:

我想使用 LINQ,但这取决于几个因素

If cbo1.SelectedIndex > -1 Then
'LINQ filter
End If

If me.cbo2SelectedIndex > -1 Then
'Filtering the LINQ again with data from the first combobox...
End If

我必须重写整个 LINQ 还是有其他方法

【问题讨论】:

  • 你能更详细地解释一下这应该做什么吗?从那模糊的 sn-p 代码中很难分辨出来……
  • 好吧,如果我要使用 dataview (dv) 那么我可以使用 dv.Filter("...") 而无需将 dim dv 写为 new dataview(dattable1, "..", "ID") 因为如果你需要“重写” LINQ,那将是很多编码。

标签: .net vb.net linq


【解决方案1】:

是的,您可以这样做。使用IQueryable 类型在顶部声明您的变量,不同部分将使用该类型,然后根据您的逻辑有选择地应用过滤器。

Dim query = InitializeQuery() ' returns type IQueryable(Of YourCustomClass)

If cbo1.SelectedIndex > -1 Then
  'LINQ filter
  query = from x in query 
          where x.Condition1 = cbo1.SelectedValue 
          select x
End If

If me.cbo2SelectedIndex > -1 Then
  'Filtering the LINQ again with data from the first combobox...
  query = from x in query 
          where x.Condition2 = cbo2SelectedIndex.SelectedValue 
          select x
End If

query 的类型始终保持不变。每个分支中唯一变化的是它的定义。

【讨论】:

    猜你喜欢
    • 2015-11-18
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    相关资源
    最近更新 更多