Linq and JSon

S

shapper

Hello,

I am filtering some tags and returning the result as JSon:

public JsonResult Filter(string q, int limit) {
var list = (from t in database.Tags
where t.Name.StartsWith(q)
orderby t.Name
select t);
List tags = (limit > 0 ? list.Take(limit) : list).ToList();
return this.Json(tags);
} // Filter

Everything works fine if the list is not empty!

If the list is empty I get an error that I caught in FireBug saying:

System.InvalidOperationException: A circular reference was detected
while serializing an object of type 'MyApp.Models.PostsTag'

How can I avoid this problem?

Thanks,

Miguel
 
J

Jon Skeet [C# MVP]

shapper said:
I am filtering some tags and returning the result as JSon:

public JsonResult Filter(string q, int limit) {
var list = (from t in database.Tags
where t.Name.StartsWith(q)
orderby t.Name
select t);
List tags = (limit > 0 ? list.Take(limit) : list).ToList();
return this.Json(tags);
} // Filter

Everything works fine if the list is not empty!

If the list is empty I get an error that I caught in FireBug saying:

System.InvalidOperationException: A circular reference was detected
while serializing an object of type 'MyApp.Models.PostsTag'

How can I avoid this problem?

Without knowing anything about PostsTag, it's pretty hard to say - but
it doesn't sound like it's got anything to do with LINQ. Just create a
new empty list without using LINQ and I'd expect you'd see the same
result.
 
S

shapper

Without knowing anything about PostsTag, it's pretty hard to say - but
it doesn't sound like it's got anything to do with LINQ. Just create a
new empty list without using LINQ and I'd expect you'd see the same
result.

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

You mean trying with List<Tag> tags = new List<Tag>();?

Well, not it does not give any error ... and the list is empty ...

I really don't get this problem ... the Linq query is not using the
PostsTag table ...

Any idea where to look at?

Thanks,
Miguel
 
J

Jon Skeet [C# MVP]

shapper said:
You mean trying with List<Tag> tags = new List<Tag>();?

Well, not it does not give any error ... and the list is empty ...

I don't mean just creating the list - I mean replacing your method body
with:

return Json(new List said:
I really don't get this problem ... the Linq query is not using the
PostsTag table ...

Any idea where to look at?

Try the above - although your original code almost certainly wasn't
exactly right, as you tried to use the List<T> class as if it weren't
generic.
 
S

shapper

I don't mean just creating the list - I mean replacing your method body
with:




Try the above - although your original code almost certainly wasn't
exactly right, as you tried to use the List<T> class as if it weren't
generic.

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

Yes, I did that ... This is very strange because I reset all my SQL
tables and everything works fine ...

The moment I associate a Tag to a Post through table PostsTags I start
having this error on the JSon Method where I use this LINQ query.

I realized that I get this error every time "q" variable in my query
is not empty ...

Any idea what might be wrong?

I looked every where but I am not able to figure this out.

Thanks,
Miguel
 
J

Jon Skeet [C# MVP]

shapper said:
Yes, I did that ... This is very strange because I reset all my SQL
tables and everything works fine ...

The moment I associate a Tag to a Post through table PostsTags I start
having this error on the JSon Method where I use this LINQ query.

I realized that I get this error every time "q" variable in my query
is not empty ...

Any idea what might be wrong?

I looked every where but I am not able to figure this out.

So where exactly is the error being thrown? Which line - the return
statement or the one before it?

I strongly suspect you can get rid of *either* Json *or* LINQ from the
equation, but it's not obvious which. If "tags" is genuinely empty, I
can't see how it could possibly matter whether that came from a LINQ
query or not.

As I said before, I suspect that's not your actual code, given that
you've used List as a nongeneric type. Could you post your *real* code?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top