Invalid LIKE query (substringof) generated via ApplysTo, yet works OK with EnableQuery attribute


Noticed a difference in the way that LINQ to SQL encodes a substringof (contains) query, depending on whether EnableQueryAttribute or ODataQueryOptions.ApplyTo is used.

For example, I send a query like the following:
And the controller looks like this:
    public IQueryable<ResourceString> GetResourceString()
        return db.ResourceStrings;
Then the LIKE query is parameterized correctly, and looks like this:
However, if I use a similar query:
And pass this to a controller like the following:
    public void BulkExportAsCsv(
        ODataQueryOptions<ResourceString> opts
            opts, AllowedQueryOptions.Filter | AllowedQueryOptions.Expand);

        var query = opts.ApplyTo(db.ResourceStrings);
                typeof(ResourceStringMap), "strings.csv");
Then the value passed to the LINQ parameter is malformed.
Notice the additional string delimiters.

This malformed query fails and returns no records on SQL Server 2014. The call to ApplyTo succeeds, but returns a non-null but empty IQueryable. I can verify that the SQL Server returns no rows. If I capture the query, remove the extra single quotes, and resubmit to SQL, then the query succeeds.

The same issue affects startswith and endswith queries, respectively:

This may be a LINQ to SQL issue, but I am wondering what EnableQueryAttribute is doing that does not trigger the bug. Is there a workaround?


jsudds wrote Jan 29, 2015 at 8:54 PM

PS: I am using System.Web.Http.OData v4.0.30319.

jsudds wrote Feb 23, 2015 at 8:11 PM