用linqToEntity(Esql)强制转换

mrJ*_*ack 1 entity-framework entity-sql c#-4.0

我的代码:

public List<Book> GetBook(string NameField, object Value)
    {
        var queryESQL = @"select VALUE Book from Book
                 where Cast(Book." + NameField + " as string) like '%M%'";
        var query = this.Entities.CreateQuery<Book>(
                  queryESQL);
        return query.ToList();
    }
Run Code Online (Sandbox Code Playgroud)

错误:

无法找到"字符串"类型.确保加载了所需的模式,并正确导入名称空间.近型名称,第2行,第51列.

更新:

新代码:

public List<Book> GetBook(string NameField, object Value)
    {
        var queryESQL = @"select VALUE Book from Book
                 where Cast(Book." + NameField + " as EDM.string) like '%M%'";
        var query = this.Entities.CreateQuery<Book>(
                  queryESQL);
        return query.ToList();
    }
Run Code Online (Sandbox Code Playgroud)

错误:

Type 'EDM.string' could not be found. Make sure that the required schemas are loaded and that the namespaces are imported correctly. Near type name, line 2, column 51.
Run Code Online (Sandbox Code Playgroud)

小智 8

CreateQuery<>方法使用CLR类型而不是EDM类型,因此使用System.String而不是EDM.String在查询中.