0

我正在尝试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 参数必须是字符串类型。近成员访问表达式

有没有其他人尝试过这个?

4

2 回答 2

1

你自己说的:

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

这就是你的答案:)

于 2012-01-11T05:00:41.437 回答
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

希望能帮助到你。

于 2012-02-16T06:22:43.170 回答