使用String.Format和PatIndex可以实现IQIeryable

时间:2014-11-03 05:16:22

标签: linq iqueryable string.format patindex

当我尝试在两个数据库实体A,B上使用string.format然后在它们上使用SqlFunctions.PatIndex时,我遇到了一个问题

     IQueryable<Data> dataRecords = DbSet<Data> M_Data;
     dataRecords = dataRecords.Where(c => SqlFunctions.PatIndex(sqlFilter, String.Format(A,B)) > 0);

抛出异常 Linq to entities无法识别方法string.Format

当我使用AsEnumarable()

dataRecords = dataRecords.AsEnumerable().Where(c => SqlFunctions.PatIndex(sqlFilter, String.Format(A,B)) > 0).AsQueryable();

投掷此函数只能从LINQ调用实体

任何人都可以建议如何做到这一点。

1 个答案:

答案 0 :(得分:0)

您有两个方法调用:

  1. String.Format
  2. SqlFunctions.PatIndex
  3. 问题是,第一个无法转换为正确的SQL查询,第二个只能在LINQ to Entities查询上下文中执行。前者使你的第一次尝试失败,后者尝试第二次尝试。

    但是,我没有看到AB成为您查询的数据的一部分。您应该能够在查询之外调用string.Format,然后使用结果:

    var formattedString = String.Format(A,B);
    dataRecords = dataRecords.Where(c => SqlFunctions.PatIndex(sqlFilter, formattedString) > 0);