Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserializing a list #2652

Closed
GaryTurpin opened this issue Mar 3, 2017 · 3 comments
Closed

Deserializing a list #2652

GaryTurpin opened this issue Mar 3, 2017 · 3 comments

Comments

@GaryTurpin
Copy link

NEST/Elasticsearch.Net version:
2.5.2
Elasticsearch version:
2.4.4
Description of the problem including expected versus actual behavior:
I am having a problem upgrading to the latest version of ES 2.X from ES 1.7 when deserializing my data. The problem only existing when trying to pull in a List<>. I know in 1.X I have it setup like the following issue #227 but it doesnt seem to work correctly for me in ES 2.X. Is there something I am missing?

ESDocument

[JsonConverter(typeof(JsonToListConverter<string>))]
public List<string> Foo { get; set; }

Getting stuff out of an IHit

public ESHit(IHit<ESDocument> hit)
{
    var ListOfFoo = hit.Fields.ValueOf<ESDocument, List<string>>(p => p.Foo);
}

Could not cast or convert from System.String to System.Collections.Generic.List`1[System.String].

@russcam
Copy link
Contributor

russcam commented Mar 6, 2017

@GaryTurpin Is the Foo field stored separately i.e. store:true on the field mapping, or are you wishing to deserialize it from _source?

@GaryTurpin
Copy link
Author

@russcam The field is stored separately. I found a workaround using:

public ESHit(IHit<ESDocument> hit)
{
    var ListOfFoo = hit.Fields.ValuesOf<string>("Foo").ToList();
}

If I uses this with lamba notation:

public ESHit(IHit<ESDocument> hit)
{
    var ListOfFoo = hit.Fields.Values<ESDocument, List<string>>(p => p.Foo);
}

it will return a List<List> opposed to List.

@russcam
Copy link
Contributor

russcam commented Mar 6, 2017

Glad you found the methods that return an array of values 👍 With .Fields.Values<TDocument, TValue>, it returns TValue[], so the following should also work

var ListOfFoo = hit.Fields.Values<ESDocument,string>(p => p.Foo).ToList();

I'm going to close this issue.

@russcam russcam closed this as completed Mar 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants