Linq 编译查询和 int[] 作为参数
我正在使用以下 LINQ to SQL 编译查询。
private static Func<MyDataContext, int[], int> MainSearchQuery =
CompiledQuery.Compile((MyDataContext db, int[] online ) =>
(from u in db.Users where online.Contains(u.username)
select u));
我知道不可能使用序列输入参数进行编译查询,并且在运行时出现“参数不能是序列”错误。
在另一篇文章相关中,我看到有一些解决方案,但是我无法理解。
有谁知道使用以数组作为输入参数的编译查询?
如果您这样做,请发布示例。
I'm using the following LINQ to SQL compiled query.
private static Func<MyDataContext, int[], int> MainSearchQuery =
CompiledQuery.Compile((MyDataContext db, int[] online ) =>
(from u in db.Users where online.Contains(u.username)
select u));
I know it is not possible to use sequence input paramter for a compiled query and im getting “Parameters cannot be sequences” error when running it.
On another post here related , I saw that there is some solution but I couldn't understand it.
Does anyone know to use complied query with array as input paramter?
Please post example if you do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就像您引用的帖子一样,它实际上不可能开箱即用。这篇文章还提到创建您自己的查询提供程序,但这会带来一些开销和复杂性,您可能不需要。
这里有几个选项:
不要使用编译的查询。相反,有一个方法将从数组中的每个项目创建一个 where 子句,从而产生如下所示的结果(伪代码):
<前><代码>在哪里
online[0] == u.用户名 ||
在线[1] == u.用户名||
...
在线[n] == u.用户名
请注意,您必须在此处使用表达式来创建每个 OR 子句。
如果您使用的是 SQL Server 2008,请创建一个标量值函数,该函数将采用 表值参数和要再次比较的值。它将返回一个位(以指示该项目是否在表中的值中)。然后通过 LINQ-to-SQL 在数据上下文中公开该函数。从那里,您应该能够为此创建一个 CompiledQuery。请注意,在这种情况下,您应该采用
IEnumerable
(假设用户名是字符串类型)而不是数组,因为您可能有多种表示字符串序列的方法,对于 SQL Server 执行此操作,顺序无关紧要。Like the post that you referenced, it's not really possible out of the box. The post also references creating your own query provider, but it's a bit of overhead and complexity that you probably don't need.
You have a few options here:
Don't use a compiled query. Rather, have a method which will create a where clause from each item in the array resulting in something like this (psuedo-code):
Note that you would have to use expression here to create each OR clause.
If you are using SQL Server 2008, create a scalar valued function which will take a table-valued parameter and a value to compare againt. It will return a bit (to indicate if the item is in the values in the table). Then expose that function through LINQ-to-SQL on your data context. From there, you should be able to create a CompiledQuery for that. Note that in this case, you should take an
IEnumerable<string>
(assuming username is of type string) instead of an array, just because you might have more than one way of representing a sequence of strings, and to SQL server for this operation, it won't matter what the order is.我发现自己正在做的一种解决方案(针对 MS SQL 2005/2008)。我不确定编写动态 sql 并使用 ExecuteQuery 方法针对数据上下文执行它是否适合所有场景。
例如,如果我有一个无界列表,我试图将其传递给查询来执行包含...
我会修改此查询以创建一个 SQL 字符串,以便像这样直接对数据库执行...
所以禁止任何语法如果我编码的错误或错误(上面或多或少是伪代码,未经测试),上面将在 SQL 中生成一个表变量,并对所需的表(本例中的个体)执行内部联接,并避免使用“IN”声明。
希望能帮助别人!
One solution that I have found myself doing (for MS SQL 2005/2008). And I'm not sure if it is appropriate in all scenarios is to just write dynamic sql and execute it against the datacontext using the ExecuteQuery method.
For example, if I have an unbounded list that I am trying to pass to a query to do a contains...
I would modify this query to create a SQL string to execute against the database directly like so...
So barring any syntax errors or bugs that I coded (the above is more or less psuedo code and not tested), the above will generate a table variable in SQL and execute an inner join against the desired table (Individuals in this example) and avoid the use of a "IN" statement.
Hope that helps someone out!