0

The code is in ASP.NET Core. MVC controller returns Json(someData). I am trying to retrieve the data in the Unit test. The best I can do is to have

string data = JsonConvert.SerializeObject(jsonResult.Value);

and then compare the string. But I would prefer to get an object or an array to be able to do some more specific comparisons. Interesting, under debugger I can see that jsonResult.Value is of type Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<MyType> and there is even Results View that warns not to expand it or else; and if I expand it, I get exactly what I want! In my case it is a 4-element array of MyType objects. However, if I do something like from i in jsonResult.Value select i I get an error

Could not find an implementation of the query pattern for source type 'object'. 
'Select' not found

I hope there is a better way than comparing a JSON string!

Felix
  • 9,248
  • 10
  • 57
  • 89

1 Answers1

1

Try using Cast method

(from i in jsonResult.Value.Cast<MyType>() select i)

Edit: updated answer

 from i in ((IQueryable< MyType >)js.Value) select i
Paresh
  • 993
  • 1
  • 7
  • 15
  • I guess you just need to cast jsonresult.Value .. try from i in ((IQueryable< MyType >)js.Value) select i – Paresh Jul 23 '16 at 04:21
  • Awesome! I tried `js.Value as IQueryable` and it didn't work; but for some reason didn't try `(IQueryable)js.Value`. Do you mind updating your answer, so I can accept it! Thanks a lot – Felix Jul 23 '16 at 04:54
  • 1
    @Felix check this answer pertaining to accessing the anonymous types http://stackoverflow.com/a/38446754/5233410 – Nkosi Jul 23 '16 at 08:10
  • @Nkosi - thank you saw much. It's so unfortunate that neither the title nor the tags mention JSON, so it never came in my search! – Felix Jul 23 '16 at 20:22