0

I am trying to implement the Like operator by using the entity framework query. But it seems Like doesn't appear to be supported on an int column.

Here is my query:

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

As you see, the Factory id is integer data type. When I execute it, it returns an error:

LIKE arguments must be of string type. Near member access expression

Did anyone else try this before?

user998405
  • 1,329
  • 5
  • 41
  • 84

2 Answers2

1

You said it yourself:

LIKE arguments must be of string type.

That's your answer :)

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

You can probably use CAST, I'm not familiar with ESQL but:

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

This link gets all mushy: How to String Concatenation in Entity SQL?

this is more formal: http://msdn.microsoft.com/en-us/library/bb399172.aspx

Hope it helps.

Community
  • 1
  • 1
Levin Magruder
  • 1,905
  • 18
  • 26