编译的LINQ和放大器; String.Contains放大器、LINQ、Contains、String

2023-09-03 05:54:03 作者:叶落琴声萧ゝ

我使用LINQ到SQL和我使用编译的LINQ获得更好的性能。

I'm using Linq-to-SQL and I use compiled Linq for better performance.

我有用户表与 INT 字段称为LookingFor,可以有以下值: 1,2,3,12,123,13 23

I have users table with a INT field called "LookingFor" that can have the following values: 1,2,3,12,123,13,23.

我写了一个查询,返回基于lookingFor列中的用户,我想返回包含lookingFor值的所有用户(不仅是相等的话)。

I wrote a query to return the users based on the "lookingFor" column and I want to return all users that contain the "lookingFor" value (not only those equal to it).

在例如,如果 user.LookingFor = 12 ,和查询放慢参数是 1 ,并且这个用户应选择

In example if user.LookingFor = 12 , and query paramter is 1, and this user should be selected.

private static Func<NeDataContext, int, IQueryable<int>>
      MainSearchQuery = CompiledQuery.Compile((NeDataContext db, int lookingFor) =>
         (from u in db.Users
          where (lookingFor == -1 ? true : u.LookingFor.ToString().Contains(lookingFor)                         
    select u.username);

这适用于非编译过LINQ,但会抛出错误使用编译时。 它使用如何解决编译的LINQ?

This WORKS on non complied linq but throws error when using compiled. How do I fix it using compiled Linq?

我得到这个错误:

这可以在客户端上进行评估的仅论据支持的String.Contains方法。

Only arguments that can be evaluated on the client are supported for the String.Contains method.

推荐答案

我面临同样的问题。我现在只有一个办法解决这个问题的方法进行管理。而不是使用

I'm facing the same problem. I have managed for now just one workaround for this problem. Instead of using

u.LookingFor.ToString().Contains(lookingFor)

我用

u.LookingFor.ToString().IndexOf(lookingFor) >= 0