How to convert ISearchResponse <dynamic> to C# Class Object?

谁都会走 提交于 2019-12-06 05:03:50

问题


How to convert ISearchResponse to C# Class Object.

I am trying to convert to Class Object where my Class name will be dynamic.

ISearchResponse<dynamic> bResponseNewLoop = 
    objElastic.Search<dynamic>(s => s
        .Index("index1")
        .Type("DOCTYPE")
        .From(0)
        .Size(10)
        .Source(sr => sr.Include(RequiredFields)));

From above Response , i want to convert the response object to class object and The class name i am retriving from xml file.


回答1:


In newer NEST versions we introduced IDocument which allows you to do lazy deserialization to the proper type.

var response = objElastic.Search<IDocument>(s => s
     .Index("index1")
     .Type("DOCTYPE")
     .From(0).Size(10)
     .Source(sr => sr.Include(RequiredFields)
);

Now on response you can loop over all the .Hits and inspect the hit metadata and use that to deserialize to the type that you want. e.g

.Hits.First().Source.As<MyDocument>()

As<>() is a method on IDocument



来源:https://stackoverflow.com/questions/31160928/how-to-convert-isearchresponse-dynamic-to-c-sharp-class-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!