ZEXSM / OData.QueryBuilder

OData.QueryBuilder - library for creating complex OData queries (OData version 4.01) based on data models with linq syntax.
MIT License
71 stars 31 forks source link

Ability to share Filter expressions between the expand level and the root level #123

Open LinusCenterstrom opened 5 months ago

LinusCenterstrom commented 5 months ago

Consider the following: You want to get top X items which have an expand with at least one value matching condition Y. For the expand you also only want to include items matching condition Y. As far as I can tell, it's currently impossible to reuse the expressions from the filter on the expand with the Any condition on the root query level which means you would have to define that condition twice.

Simple example of how I imagine the call would look (using your test classes)

Expression<Func<string, bool>> isTestTag = t => t == "testTag";

var uri = _odataQueryBuilderDefault
    .For<ODataTypeEntity>(s => s.ODataType)
    .ByList()
    .Expand(x => x.For<string>(t => t.Tags).Filter(isTestTag))
    .Filter((s, f, o) => o.Any(s.Tags, isTestTag))
    .ToUri();

uri.Should().Be("http://mock/odata/ODataType?$expand=Tags($filter='testTag')&$filter=Tags/any(t:t eq 'testTag')");