473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3699
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: G Matthew J | last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm This is more or less in response to Mr Crockford's admonition a few months ago, "fork if you must". Ironically, although in that usenet post...
20
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
2
by: Kevin Newman | last post by:
Hello, I noticed that the JavaScript library for JSON posted on json.org (http://www.json.org/json.js) is modifying Object.prototype (adding a method - toJSONString). I thought this was...
3
by: Adam | last post by:
I'm trying to retrieve some values from a json object, but instead it's giving me the property name. For example: var json = { "glossary": { "title": "example glossary" } }; console.log(json);...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
23
by: dhtmlkitchen | last post by:
JSON We all know what it is. In ECMAScript 4, there's a JSON proposal: Object.prototype.toJSONString String.prototype.parseJSON The current proposal, String.prototype.parseJSON, returns...
9
by: Jon Paal [MSMD] | last post by:
using json like ( {"Records": , "RecordCount":"1" } ) and jquery like: $.ajax({ .... success: function(json, status) {
6
by: Lasse Reichstein Nielsen | last post by:
Max <adsl@tiscali.itwrites: Not really. It shows that a particularly naïve implementation of a conversion from XML to JSON doesn't work well. What if the conversion of <e> some
0
by: crocodilu2008 | last post by:
JSON vs. XML JSON and XML are basically used for the same purpose—to represent and interchange data. I'll try to show you why you might want to use JSON rather than XML in an AJAX context by showing...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.