2

I am trying to change a few fields on an autoquery to query using or (it is a search box that is searching many fields). This doesn't seem to work although according to the documentation it should.

public class PropertyGet : QueryDb<DomainModel.Property>
{
    [QueryDbField(Term=QueryTerm.Or)]
    public string NameContains { get; set; }

    [QueryDbField(Term=QueryTerm.Or)]
    public string CityContains {get;set;}
}

However this does:

[QueryDb(QueryTerm.Or)]
 public class PropertyGet : QueryDb<DomainModel.Property>
    {
        public string NameContains { get; set; }
        public string CityContains {get;set;}
    }

Changing Querying Behavior

By default queries act like a filter and every condition is combined with AND boolean term to further filter the result-set. This can be changed to use an OR at the field-level by specifying Term=QueryTerm.Or modifier, e.g:

[QueryDbField(Term=QueryTerm.Or)] public string LastName { get; set; }

How can I do a field level Or query?

lucuma
  • 18,247
  • 4
  • 66
  • 91
  • What do you mean by "field level Or query"? The first query needs to be a filter in a WHERE statement. What SQL are you hoping to generate? – mythz Mar 02 '17 at 16:18
  • I was only trying to filter based on or's instead of and's. In my case, where Name contains or City contains. There will be other fields that will have `and` conditions. I was expecting the first case to work before I started plugging in the and conditions. – lucuma Mar 02 '17 at 16:19
  • This is all in relation to a kendo grid. The columns are filterable (AND) and there is a search box above the grid that I want to produce a set of OR conditions on the same autoquery. – lucuma Mar 02 '17 at 16:21

1 Answers1

2

The issue is due to using a [QueryDbField] that relies on an implicit convention like "%Contains":

public class PropertyGet : QueryDb<DomainModel.Property>
{
    [QueryDbField(Term=QueryTerm.Or)]
    public string NameContains { get; set; }

    [QueryDbField(Term=QueryTerm.Or)]
    public string CityContains {get;set;}
}

Where when using [QueryDbField] you would override the implicit convention. I've changed the behavior in this commit where it now merges the behavior of both [QueryDbField] and the matching implicit convention so your query should now work as expected.

This change is available from v4.5.7+ that's now available on MyGet.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • The only way I could get this to work was by specifying the entire db field defintion ` [QueryDbField(Field = "Name", Template = "{Field} LIKE {Value}", ValueFormat = "%{0}%", Term=QueryTerm.Or)]` Just adding the `QueryTerm.Or` doesn't work for me – lucuma Mar 02 '17 at 16:40
  • @lucuma oh you mean for changing the behavior to Contains, yeah if you're supplying your own `[QueryDbField]` you're taking over the behavior and need to change it from its default `=` behavior. I'll see if it's easy to retain the implicit convention behavior. – mythz Mar 02 '17 at 16:44
  • @lucuma Not how it's designed, i.e. if you use `[QueryDbField]` you're taking over complete behavior of the field so you need to specify how you want it to behave. – mythz Mar 02 '17 at 16:55
  • @lucuma FYI see updated answer where the behavior is now modified so it combines both QueryDbField and any matching implicit convention. – mythz Mar 02 '17 at 17:35
  • 1
    @lucuma no, they get deployed to NuGet - we'll deploy .NET Core early next week. – mythz Mar 02 '17 at 17:56