Convert type 'System.Dynamic.DynamicObject to System.Collections.IEnumerable

不羁岁月 提交于 2019-12-05 03:34:08

DynamicObject is inherited from IDictionary, so you can cast it to IDictionary.

public IDictionary<string, object> GetEntities(string entityName, string entityField)
    {
       var serializer = new JavaScriptSerializer();
        serializer.RegisterConverters(new[] { new MyProject.Extensions.JsonExtension.DynamicJsonConverter() });
       dynamic data = serializer.Deserialize(json, typeof(object));
       return data as IDictionary<string, object>;
    }




foreach (var author in GetEntities("author", "lastname"))

Given your example usage of 'GetEntities', try changing its return type to IEnumerable<T> (or, although strongly not recommended, at least an IEnumerable<dynamic>). You would need to do some filtering within the method to extract the appropriate entities based on the 'entityName' input parameter. Although, it's unclear what the intended usage is of the other input parameter ('entityField').

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