编译查询中不支持字符串数组

时间:2016-08-24 12:37:03

标签: c# linq

我必须在c#的编译查询中传递字符串数组但不支持。  你能用另一种方式给我吗?

错误:类型为“System.String []”的指定参数“searchStrings”无效。仅支持标量参数(例如Int32,Decimal和Guid)。

 static Func<ApplicationEntitiesEntities, int, int, string,string[], IEnumerable<DAL.Participant>> GetSearchParticipantModelCompile =
       CompiledQuery.Compile<ApplicationEntitiesEntities, int, int, string,string[], IEnumerable<DAL.Participant>>(
          (db, startIndex, recordCount, searchString,searchStrings) => (from s in db.Participants
                                                          where s.IsDeleted == false &&
                                                          searchStrings.Contains(s.MEMBERSHIP_Users.FirstName))
                                                          orderby s.MEMBERSHIP_Users.LastName
                                                          select s).Skip(startIndex).Take(recordCount));

1 个答案:

答案 0 :(得分:0)

最终,编译后的查询希望尽可能提前做好准备工作。当输入数据具有varadic大小时,此根本不可能,因为对于0项,1项,2项和200项,查询看起来可能非常不同。因此,CompiledQuery可能拒绝允许此类查询。选项:

  • 通过CompiledQuery预先生成一些密钥大小,并使用switch / .Count上的.Length选择其中一个预编译查询,default使用未预编译的查询;来自Stack Overflow的一个真实例子:

    static readonly Func<DBContext, int, Post> _get1 =
        CompiledQuery.Compile((DBContext db, int id) => db.Posts.SingleOrDefault(p => p.Id == id));
    static readonly Func<DBContext, int, int, IEnumerable<Post>> _get2 =
        CompiledQuery.Compile((DBContext db, int id0, int id1) => db.Posts.Where(p => p.Id == id0 || p.Id == id1));
    static readonly Func<DBContext, int, int, int, IEnumerable<Post>> _get3 =
        CompiledQuery.Compile((DBContext db, int id0, int id1, int id2) => db.Posts.Where(p => p.Id == id0 || p.Id == id1 || p.Id == id2));
    static readonly Func<DBContext, int, int, int, int, IEnumerable<Post>> _get4 =
        CompiledQuery.Compile((DBContext db, int id0, int id1, int id2, int id3) => db.Posts.Where(p => p.Id == id0 || p.Id == id1 || p.Id == id2 || p.Id == id3));
    static readonly Func<DBContext, int, int, int, int, int, IEnumerable<Post>> _get5 =
        CompiledQuery.Compile((DBContext db, int id0, int id1, int id2, int id3, int id4) => db.Posts.Where(p => p.Id == id0 || p.Id == id1 || p.Id == id2 || p.Id == id3 || p.Id == id4));
    static readonly Func<DBContext, int, int, int, int, int, int, IEnumerable<Post>> _get6 =
        CompiledQuery.Compile((DBContext db, int id0, int id1, int id2, int id3, int id4, int id5) => db.Posts.Where(p => p.Id == id0 || p.Id == id1 || p.Id == id2 || p.Id == id3 || p.Id == id4 || p.Id == id5));
    

       switch (remaining)
        {
            case 1: results.Add(_get1(Current.DB, ids[offset++])); break;
            case 2: results.AddRange(_get2(Current.DB, ids[offset++], ids[offset++])); break;
            case 3: results.AddRange(_get3(Current.DB, ids[offset++], ids[offset++], ids[offset++])); break;
            case 4: results.AddRange(_get4(Current.DB, ids[offset++], ids[offset++], ids[offset++], ids[offset++])); break;
            case 5: results.AddRange(_get5(Current.DB, ids[offset++], ids[offset++], ids[offset++], ids[offset++], ids[offset++])); break;
            default: results.AddRange(_get6(Current.DB, ids[offset++], ids[offset++], ids[offset++], ids[offset++], ids[offset++], ids[offset++])); break;
        }
    

    (是的,真的)

  • 手动编写SQL,可能使用SQL Server 2016中的STRING_SPLIT feature来允许您传入带有分隔值的单个参数。如果这不可用,您可以使用“表值参数”,或者您可以生成适合特定运行时查询的TSQL和参数。