Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid cast exception when using inlinecount #30

Closed
mbonig opened this issue Apr 16, 2014 · 5 comments
Closed

Invalid cast exception when using inlinecount #30

mbonig opened this issue Apr 16, 2014 · 5 comments

Comments

@mbonig
Copy link

mbonig commented Apr 16, 2014

Trying to use inline count:

var asQueryable = new List
{
"one",
"two",
"three"
}.AsQueryable();

        var linqToQuerystring = asQueryable.LinqToQuerystring("?$top=1&$skip=1&$inlinecount=allpages");

throws a:

System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.Dictionary2[System.String,System.Object]' to type 'System.Linq.IQueryable1[System.String]'.

Expected to get results with "3" and "two".

if I remove the inlinecount piece no error is thrown.

@mbonig
Copy link
Author

mbonig commented Apr 16, 2014

This is my current nuget package:

<package id="LinqToQuerystring" version="0.7.0.7" targetFramework="net45" />

@beyond-code-github
Copy link
Owner

Hi @mbonig,

In this situation I think you need to pass a couple of type parameters to the LinqToQuerystring extension method to help it along:

.LinqToQuerystring<string, Dictionary<string, object>>("?$top=1&$skip=1&$inlinecount=allpages");

The reason for this is that it has to return a structure containing the count and the records, and the dictionary was the easiest way to do this. I'm looking at ways of making this slightly nicer though.

Let me know if this helps

@mbonig
Copy link
Author

mbonig commented Apr 16, 2014

I'm a bit confused then. I understand it returning that type, but what is the expected behavior if the string being passed in does not contain inlinecount? Should I always expect a dictionary regardless?

@beyond-code-github
Copy link
Owner

Because the common use case for Linq to Querystring is that the output is immediately serialized to json or xml for transfer down the wire, you can usually rely on the serialiser to take the ouput and deal with it regardless of the actual type.

You can also use the non-generic overload...

object result = asqueryable.LinqToQuerystring(typeof(string), "?$top=1&$skip=1&$inlinecount=allpages");

...which returns an object so you can deal with this scenario or even check to see if you've got a dictionary or a queryable back if you need to.

@mbonig
Copy link
Author

mbonig commented Apr 16, 2014

Thanks! I'm not doing the usual case here, as I need to apply this query pretty low in code.

I'm going to do everything around object and handle the type, because I want the inline count returned in a different manner.

Thanks!

@mbonig mbonig closed this as completed Apr 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants