本文介绍了无法将类型'Newtonsoft.Json.Linq.JObject'隐式转换为'System.Collections.Generic.IEnumerable< Employee>'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

public EmployeeDepartment InsertEmployeeAndDepartment(params dynamic[] employee)
{                
    filteredEmployees.EmployeeRecords = employee[0];           
}

EmployeeRecords 的类型为IEnumerable<Employee>

我遇到以下异常:

我该如何解决?

推荐答案

该消息表明employee[0]作为JObject返回,因此必须对其进行解析才能转换为其他类型的对象.我还没有看到足够多的代码来知道哪种代码最适合您,因此您可能需要采取更多步骤,但是有些东西从动态到强类型映射(使用JObject.ToObject())或强类型JSON解析(使用JsonConvert.DeserializeObject()),如下页所述,应该使您走上正确的路:

The message suggests that employee[0] is being returned as a JObject, so it has to be parsed in order to be cast to another type of object. I haven't seen enough of your code to know which one will work best for you, and you may have more steps to take, but something like the Dynamic to Strong Type Mapping (using JObject.ToObject()) or Strongly Typed JSON Parsing (using JsonConvert.DeserializeObject()) as described on the following page should put you on the right path:

http ://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing#DynamictoStrongTypeMapping

这篇关于无法将类型'Newtonsoft.Json.Linq.JObject'隐式转换为'System.Collections.Generic.IEnumerable&lt; Employee&gt;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:09