用linqToEntity(Esql)强制转换

时间:2011-08-25 05:54:49

标签: entity-framework c#-4.0 entity-sql

我的代码:

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();
    }

错误:

  

找不到类型'字符串'。确保所需的模式   加载并正确导入名称空间。近型   名称,第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();
    }

错误:

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.

2 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:2)

投射时使用Edm.String代替string