Operator '??' cannot be applied to operands of type IQueryContainer and lambda expression

194 views Asked by At

I am trying to create a method to process a certain query. I follow an example posted on the Nest repository (line 60), but still the MatchAll is not recognized by the compiler and if I try to build the solution, the error that shows is:

Operator '??' cannot be applied to operands of type IQueryContainer and lambda expression

This is my method so far:

public void ProcessQuery(IQueryContainer query = null)
{

   var searchResult = this._client.Search<T>(
                s => s
                    .Index(MyIndex)
                    .AllTypes()
                    .From(0)
                    .Take(10)
                    .Query(query ?? (q => q.MatchAll())) // Not valid
                    .SearchType(SearchType.Scan)
                    .Scroll("2m")
                );
}
2

There are 2 answers

0
Yasel On BEST ANSWER

Thanks to the comment of @Mrinal Kamboj and the answer of @Wormbo, I found my own answer:
I changed the argument type to QueryContainer and if the argument is null, a new QueryMatchAll query is created, this works for me:

public void ProcessQuery(QueryContainer query = null)
{

   var searchResult = this._client.Search<T>(
                s => s
                    .Index(MyIndex)
                    .AllTypes()
                    .From(0)
                    .Take(10)
                    .Query(query ?? new MatchAllQuery()) // Now works
                    .SearchType(SearchType.Scan)
                    .Scroll("2m")
                );
}
0
Wormbo On

The type of a lambda expression can either be converted to an Expression or to some delegate type, but most likely not to IQueryContainer. Lambda expressions themselves do not have a type and need specific context for that automatic conversion, which you can give e.g. by using an appropriate delegate type constructor. But again: I don't believe an interface on one side of ?? and a lambda expression on the other makes any kind of sense.