我已经编写了一些看起来很简单的LINQ to SQL代码:
Dim TestAcct = (
From s In ITS_DB.SysValues
Where s.vTable = STR_BQSync And s.Category = STR_TestAcct And s.Comment = STR_N).ToList
' other code
Dim _ChangedAccounts = (
From c In FinanceDB.Customers
join t In TestAcct On t.Value Equals c.Customer_Account.ToString
Select c).ToList
问题是在运行时第二个抛出异常:
本地序列不能用于LINQ to SQL的查询实现 除Contains运算符之外的运算符
我已经看过一些关于使用contains的讨论,但我不确定是否适用于这种情况。异常消息中引用的本地序列是什么?
答案 0 :(得分:1)
从ToList
的作业中删除TestAcct
。这将把它作为查询,并允许第二个查询被Linq to SQL解析为更好的最终SQL查询。
Dim TestAcct = (
From s In ITS_DB.SysValues
Where s.vTable = STR_BQSync And s.Category = STR_TestAcct
And s.Comment = STR_N)
' other code
Dim _ChangedAccounts = (
From c In FinanceDB.Customers
join t In TestAcct On t.Value Equals c.Customer_Account.ToString
Select c).ToList
答案 1 :(得分:0)
您应该只能从第一个集合中投射一组连接键,然后在第二个查询中的匹配键上使用.Contains
:
Dim TestAccts = (
From s In ITS_DB.SysValues
Where s.vTable = STR_BQSync And s.Category = STR_TestAcct And s.Comment = STR_N
Select s.Value).ToList
然后
Dim _ChangedAccounts = (
From c In FinanceDB.Customers
Where TestAccts.Contains(c.Customer_Account.ToString)
Select c).ToList
答案 2 :(得分:0)
我做了一些测试,并提出了一个有趣的解决方案!
Dim TestAcct = (
From s In ITS_DB.SysValues
Where s.vTable = STR_BQSync And s.Category = STR_TestAcct And s.Comment = STR_N).ToList
' some other code
_ChangedAccounts = (
From t In TestAcct
join c In FinanceDB.Customers On t.Value Equals c.Customer_Account.ToString
Select c).ToList
第一个查询未更改。在第二个我简单地颠倒了我正在使用的表的顺序。它工作正常!我不确定为什么,但它非常简单。