【问题标题】:Dynamic Where in Linq to SQLLinq to SQL 中的动态 Where
【发布时间】:2011-04-12 06:59:44
【问题描述】:

我怎样才能让这样的事情起作用,以便我可以动态地改变这个 linq to sql 查询中的 where 子句?

Dim AccountID = 1234

Dim AccountList

Select Case Types
    Case 1
        AccountList = (from a in dc.Accounts where a.ID = AccountID)
    Case 2
        AccountList = (from a in dc.Accounts where a.ID = AccountID And a.ParentID = AccountID)
    Case Else
        AccountList = (from a in dc.Accounts)
End Select

Return From p in dc.Products where AccountList.Contains(p.Order.Transaction.AccountID)

使用上面的代码我得到这个错误:

后期绑定操作无法转换为表达式树

【问题讨论】:

    标签: vb.net linq linq-to-sql dynamic where-clause


    【解决方案1】:

    听起来您要么没有启用 Option Strict。戴上它,将AccountList的类型指定为IQueryable(Of Account)。那么你就不会使用后期绑定了,一切都会好起来的。

    【讨论】:

    • AccountList 实际上只是一个字符串列表,因此将其设为 IQueryable(of String) 就可以了 - 谢谢!
    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 2011-10-03
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多