0

I am using a SQL Server database, Entity Framework 6, OData Webservice 4 and ASP.NET Web API 2.2 for OData.

To fetch specific objects from the remote server, the call looks like this:

DataServiceContext context = ...;
DataServiceQuery query = context.CreateQuery<SomeType>(...);
Expression<Func<SomeType, bool>> filterExpression = 
          item => item.SomeID.ToString().Contains("23");

IQueryable<SomeType> queryResult = query;

queryResult = queryResult.Where(filterExpression);
queryResult.ToList();

This goes wrong on execution because ToString() is not possible to the SomeID field, which is an integer.

But the OData definition has something like cast(..., Edm.String) to be used in the query URL. The framework just doesn't use it, when I use ToString() on the LINQ side.

(I can't even send the stupid filterExpression via a DataServiceActionQuery to handle it inside the controller on the service side. Seems like

Expression<Func<...>>

is not supported to be passed as a BodyOperationParameter to the service action.)

What can be done? How can it be done?

My am is a fulltext search among numeric fields, returning only matching datasets of SomeType.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Satria
  • 315
  • 2
  • 9
  • I can't test this for now, but a fast answer could be add a property en SomeType class, that return your SomeID in String type and then use it in your expression. – Juan Ruiz de Castilla Sep 10 '15 at 16:22
  • Have tried creating a Model-Defined Function, which you can actually use in you in Queries (as a method). Then intead of trying to pass the expression to the service, pass in tuples of property name to the value to compare with (and build the expression on the service side using the model defined function). – TYY Sep 10 '15 at 17:01
  • That's an interesting way of doing it. If I got you right, this comes close to writing my own query language though... Let's see, what I can make out of the general idea of yours. Thanks! – Satria Sep 11 '15 at 14:16
  • Problem is, that the Get-mechanism on the service side is a scaffolded one, and I cannot affect what kind of parameters it expects. What the controller actually does with the expression contained in the serviceQuery is not exposed... :( – Satria Sep 11 '15 at 14:27

1 Answers1

0

I can't test this for now, but a fast answer could be add a property in SomeType class, that return your SomeID in String type and then use it in your expression, something like this:

public class SomeType
{
  public int SomeID { set; get; }

  public string someIDStr
  {
    get { return this.SomeID.ToString(); }
  }
}

Expression<Func<SomeType, bool>> filterExpression = item => item.someIDStr.Contains("23");
  • Nice idea. I though so, too. But the server throws an exception, because someIDStr is not part of the entity I suppose, the real DB is queried here and not the entity model. Except something went wrong with the compilation of the Webservice and it didn't get the new property, when I tested this. But I explicitly re-compiled the service before I started it to test again. So I think, this option is a dead end. – Satria Sep 10 '15 at 16:44
  • if you try to not map the property in your entity model, i dont have how test it rigth now, but maybe this can help you: http://stackoverflow.com/questions/1987836/creating-a-non-mapped-property-in-an-entity-entity-framework – Juan Ruiz de Castilla Sep 10 '15 at 16:51
  • Well, of course I didn't add the string property into my generated entity classes, but instead put it into a partial class in a different file. But that doesn't help that the service still doesn't accept it ;) – Satria Sep 10 '15 at 16:53
  • this sounds more an error of refresh or rebuild, but i can't test it like i said :(. – Juan Ruiz de Castilla Sep 10 '15 at 16:58
  • No success... I tried [NotMapped] annotation as well as [DatabaseGenerated(DatabaseGeneratedOption.Computed)] It says: "The query specified in the URI is not valid. Fuer den Typ 'SomeNamespace.SomeType' wurde keine Eigenschaft mit dem Namen 'SomeID_str' gefunden." (= For type ...SomeType no property named SomeID_str was found.) – Satria Sep 11 '15 at 13:08