WCF/WF - Returning IQueryable filtered through CreateQuery

Asked By Jarrod Krige on 03-Jan-11 03:42 AM
Hello,

Please help me implement ObjectContext.CreateQuery. I get an error:

System.ServiceModel.DomainServices.Client.DomainOperationException: Load operation failed for query 'GetViewStores'. 'strCode' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near simple identifier, line 1, column 38.


public class ForeshareBusinessIntelligenceDomain : LinqToEntitiesDomainService<FG_CoreEntities>
  {
    public IQueryable<viewStore> GetViewStores(string strCode, string strName, string strGroup, string strType, string strCluster, string strChannel, string strArea, string strDisabled)
    {
      string strQuery = "select strName from viewStores where ";
      if (!strCode.StartsWith("("))
      {
        strQuery += "strCode = @strCode ";
      }
      ObjectParameter op = new ObjectParameter("strCode", strCode);
      return ObjectContext.CreateQuery<viewStore>(strQuery, op);
    }
  }


I am trying to filter the results of a SQL View dynamically depending on user selections from drop-down lists. The query will be built depending on the user selection by parsing the selection to the function and then building the T-SQL query as a string and then return the result of CreateQuery().

Thanks.
Indranil Chatterjee replied to Jarrod Krige on 11-Jan-11 07:07 AM
In Entity sql, you prob'ly cannot direcly reference field names without an alias.
Try using this query instead:

string strQuery = "select value vs.strName from viewStores as vs where ";
    if (!strCode.StartsWith("("))
    {
    strQuery += "vs.strCode = @strCode ";
    }
    ObjectParameter op = new ObjectParameter("strCode", strCode);
    return ObjectContext.CreateQuery<viewStore>(strQuery, op);