实体查询'Like'不支持int列

时间:2012-01-11 04:57:13

标签: c# entity-framework-4 sql-like entity-sql

我正在尝试使用实体框架查询来实现Like运算符。但似乎Like列似乎不支持int

这是我的问题:

using (ObjectContext ctx = new ObjectContext(gbcDbConnection.eObjqueryConnection))
{
    string result = " Select ";
    result += "       master_factory.FACTORY_ID, master_factory.FACTORY_NAME,  ";
    result += "       case when master_factory.FACTORY_TYPE ='"  
           + gbvArticleVarieble.FactoryTypeExterIni+"' then '" 
           + gbvArticleVarieble.FactoryTypeExter+"' else '" 
           + gbvArticleVarieble.FactoryTypeInter+"' end as factory_type ";

    result += " FROM  ";
    result += "     LEWREDBEntities.[MASTER_FACTORY] as master_factory  ";


    result += " WHERE ";
    result += "      (master_factory.FACTORY_TYPE = @factoryType1 or master_factory.FACTORY_TYPE =@factoryType2) and master_factory.STATUS<>@status";


    if (searchVal != "")
    {
         result += "      AND master_factory.[FACTORY_ID]  LIKE '%' + @searchVal + '%' ";
    }



    ObjectQuery<DbDataRecord> query = ctx.CreateQuery<DbDataRecord>(result);
    query.Parameters.Add(new ObjectParameter("status", gbcDBVariable.DeleteIni));
    query.Parameters.Add(new ObjectParameter("factoryType1",gbvArticleVarieble.FactoryTypeExterIni ));
    query.Parameters.Add(new ObjectParameter("factoryType2", gbvArticleVarieble.FactoryTypeInterIni));
    if (searchVal != "")
    {
        query.Parameters.Add(new ObjectParameter("searchValue", searchVal));

    }
    if (logger.IsDebugEnabled)
    {
        query.ToTraceString();
    }

如您所见,Factory id是整数数据类型。当我执行它时,它返回一个错误:

  

LIKE参数必须是字符串类型。近成员访问表达式

之前有没有人尝试过这个?

2 个答案:

答案 0 :(得分:1)

你自己说:

  

LIKE参数必须是字符串类型。

这是你的答案:)

答案 1 :(得分:1)

您可以使用CAST,我不熟悉ESQL,但是:

AND CAST(master_factory。[FACTORY_ID] as System.String)LIKE'%'+ @ searchVal +'%'

这个链接变得糊涂了: How to String Concatenation in Entity SQL?

这更正式: http://msdn.microsoft.com/en-us/library/bb399172.aspx

希望它有所帮助。