我有一个动作要搜索的人的控制,它会返回anonymouse的列表:无法将IEnumerable的匿名类型转换为IEnumerable <dynamic>?
[UnitOfWork]
[HttpGet, Route("api/Search/People")]
public virtual IHttpActionResult GetResult(string keyword)
{
// ...
var result = peopleList.Select(x => new
{
PersonId = x.Id.Value,
EmploymentNumber = x.EmploymentNumber,
FirstName = x.Name.FirstName,
LastName = x.Name.LastName,
Email = x.Email
});
return Ok(result);
}
下面是该方法的测试案例:
[Test]
public void ShouldSearchPeople()
{
// Mocks...
var target = new PeopleSearchController(searchRepository, personRepository, new FakePermissionProvider());
// Error here
var result = (OkNegotiatedContentResult<IEnumerable<dynamic>>)target.GetResult("Ashley");
Assert.NotNull(peopleList);
// Other assert...
}
然后我得到如下错误:
System.InvalidCastException: Unable to cast object of type
'System.Web.Http.Results.OkNegotiatedContentResult`1[System.Collections.Generic.IEnumerable`1[<>f__AnonymousType1e`5[System.String,System.String,System.String,System.Guid,System.String]]]'
to type
'System.Web.Http.Results.OkNegotiatedContentResult`1[System.Collections.Generic.IEnumerable`1[System.Object]]'.
看起来像IEnumerable类或单匿名可以这样转换,但IEnumerable与匿名不起作用。
我该怎么做这样的转换?谢谢。
也许'var result = target.GetResult(“Ashley”)为OkNegotiatedContentResult>;'在你的测试方法中适合你......? –
stefankmitph
2015-04-03 06:53:13
@stefankmitph'result'将始终为空。 – 2015-04-03 07:07:39