Cannot convert lambda expression to type 'PostData' because it is not a delegate type

I have this code in my .net core app

var searchResponse = client.Search<Person>(s => s
.From(0)
.Size(10)
.Query(q => q
     .Match(m => m
        .Field(f => f.FirstName)
        .Query("Martijn")
     )
)

);

which I got from this page https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nest-getting-started.html

I'm getting an error at design time "Cannot convert lambda expression to type 'PostData' because it is not a delegate type". I have referenced Elasticsearch and System.Linq.

What am I missing?

I've also tried this syntax from https://www.elastic.co/guide/en/elasticsearch/client/net-api/1.x/nest-quick-start.html

QueryContainer query = new TermQuery

{
Field = "firstName",
Value = "martijn"
};

var searchRequest = new SearchRequest
{
From = 0,
Size = 10,
Query = query
};

And now I get an error "cannot convert from 'Nest.SearchRequest' to 'Elasticsearch.Net.PostData"

I'm not sure why it is so hard to make a simple API call here.

First thing to check, are the NEST and Elasticsearch.Net NuGet packages referenced both the same version number?

Yes, they are both Version 6.2.0.

I've managed to use the ElasticLowLevelClient successfully to Index, but if I use that to Count or Search since it seems to just do a post, I can't map the response object to actually use the data I'm trying to get.

Ok. What is the type of client above? Would you be able to provide a short, but complete, example of the issue?

Also, would you be able to provide more details of the environment

  • Windows, MacOS, Linux?
  • framework TFM e.g. netcoreapp2.0

Hi, so this is embarrassing. I was wanting to use the Low Level Client, but that was obviously not right. I then tried adding the Nest reference and changing to High Level Client but that didn't work either.
Then I closed VS Code, reopened it and all of a sudden it works. Frustrating!

For info, in case anyone else has this -
Running on Windows
Elasticsearch v6.3.0 running locally (just for dev purposes)
Developing in C# (dotnet core), using VSCode.

Code now looks like:

var node = new Uri(Config.Configuration["elasticsearch:0:uri"]);
var settings = new ConnectionSettings( node );
Client = new ElasticClient(settings);
QueryContainer query = new MatchQuery
{
	Field = keyValue.Key,
    Query = keyValue.Value.ToString()
};
var searchRequest = new SearchRequest(index.IndexName, index.IndexType)
	{
		Query = query
	};
var searchResults = Client.Search<object>(searchRequest);

Thanks again!

Glad you figured it out @StephM :+1:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.