如何转换ISearchResponse< dynamic>到C#类对象? [英] How to convert ISearchResponse <dynamic> to C# Class Object?

查看:146
本文介绍了如何转换ISearchResponse< dynamic>到C#类对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换 ISearchResponse 转换为C#类对象.

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)));

从Response上方,我想将响应对象转换为class对象,然后从xml文件中检索该类名.

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

推荐答案

在较新的NEST版本中,我们引入了IDocument,它允许您对适当的类型进行延迟反序列化.

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)
);

现在,在响应时,您可以遍历所有.Hits并检查匹配的元数据,并使用它来反序列化为所需的类型.例如

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<>()IDocument

这篇关于如何转换ISearchResponse&lt; dynamic&gt;到C#类对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆