我有一個動作要搜索的人的控制,它會返回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