473,239 Members | 1,607 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,239 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 3696
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
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...

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.