469,358 Members | 1,628 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

Linq and JSon

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

Sep 3 '08 #1
5 3574
shapper <md*****@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 3 '08 #2
On Sep 3, 9:19*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
shapper <mdmo...@gmail.comwrote:
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.

--
Jon Skeet - <sk...@pobox.com>
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<Tagtags = 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
Sep 3 '08 #3
shapper <md*****@gmail.comwrote:
You mean trying with List<Tagtags = 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<Tag>());
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<Tclass as if it weren't
generic.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 3 '08 #4
On Sep 3, 9:54*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
shapper <mdmo...@gmail.comwrote:
You mean trying with List<Tagtags = 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<Tag>());
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<Tclass as if it weren't
generic.

--
Jon Skeet - <sk...@pobox.com>
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
Sep 3 '08 #5
shapper <md*****@gmail.comwrote:
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?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 4 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by G Matthew J | last post: by
20 posts views Thread by Luke Matuszewski | last post: by
2 posts views Thread by Kevin Newman | last post: by
3 posts views Thread by Adam | last post: by
23 posts views Thread by dhtmlkitchen | last post: by
9 posts views Thread by Jon Paal [MSMD] | last post: by
6 posts views Thread by Lasse Reichstein Nielsen | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.