Hi There, I'm receiving a circular reference error when I use sum in my linq query and return as JSON. I've tried various ways to re-write the code and I've also tried turning off the lazy loading for entities. None of this works but when I stop using sum
the code is fine.
Can anyway provide a workaround for this please?
var al = (from crtProd in dbContext.tblCartSessions_Products
where crtProd.sessionID == context.Session["id"].ToString()
group crtProd by crtProd.productPrice into g
select new { Numb = g.Sum(p => p.productPrice) }).FirstOrDefault();
if you are returning any collection make sure to return it in List<> or class when there is single record. This will help you to avoid such problems.
entity data frameworkLinq
Ashutosh Pathak
Blog: http://catchcode.blogspot.com Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
Thanks for answering the question for me. I'm not using MVC for this, I'm using a a generic handler. I'm only needing to return a couple of values not a whole collection. How can I write me code to work and not throw this exception?
in such case create a class, make some prooperties and put the values inside from your data source, and return this class from your code.
Ashutosh Pathak
Blog: http://catchcode.blogspot.com Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
The statement returns multiple values, sumed into one. How would I write this if I wanted to return a single value as a decimal?
shopCart.subTotal = (from crtProd in dbContext.tblCartSessions_Products
where crtProd.sessionID == context.Session["id"].ToString()
group crtProd by crtProd.productPrice into g
select new { Numb = g.Sum(p => p.productPrice) });
I get the error:
Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'decimal?'
shopCart.subTotal = (from crtProd in dbContext.tblCartSessions_Products where crtProd.sessionID == context.Session["id"].ToString() group crtProd by crtProd.productPrice into g select new { Numb = g.Sum(p => p.productPrice) }).FirstOrDefault().Numb;
bullrout@gma...
Member
5 Points
49 Posts
a circular reference was detected while serializing an object of type 'System.Reflection.Runtime...
Oct 28, 2012 12:57 AM|LINK
Hi There, I'm receiving a circular reference error when I use sum in my linq query and return as JSON. I've tried various ways to re-write the code and I've also tried turning off the lazy loading for entities. None of this works but when I stop using sum the code is fine.
Can anyway provide a workaround for this please?
var al = (from crtProd in dbContext.tblCartSessions_Products
where crtProd.sessionID == context.Session["id"].ToString()
group crtProd by crtProd.productPrice into g
select new { Numb = g.Sum(p => p.productPrice) }).FirstOrDefault();
Decimal? test = al.Numb;
entity data framework Linq
ignatandrei
All-Star
135204 Points
21687 Posts
Moderator
MVP
Re: a circular reference was detected while serializing an object of type 'System.Reflection.Run...
Oct 28, 2012 02:26 AM|LINK
runtime what?
and I think that when you do not use sum is wrong...
thaicarrot
Contributor
5132 Points
1465 Posts
Re: a circular reference was detected while serializing an object of type 'System.Reflection.Run...
Oct 28, 2012 05:22 AM|LINK
If this is Web APIs so that there's a problem with circular referenced, I forgot the link just solve it with JSON way.
Weera
bullrout@gma...
Member
5 Points
49 Posts
Re: a circular reference was detected while serializing an object of type 'System.Reflection.Run...
Oct 28, 2012 07:12 AM|LINK
Hi there, it's saying
a circular reference was detected while serializing an object of type
'System.Reflection.RuntimeModule'
I'm not sure what you mean by not using sum?
Ashutosh Pat...
Contributor
5737 Points
1105 Posts
Re: a circular reference was detected while serializing an object of type 'System.Reflection.Run...
Oct 28, 2012 07:34 AM|LINK
if you are returning any collection make sure to return it in List<> or class when there is single record. This will help you to avoid such problems.
entity data framework Linq
Blog: http://catchcode.blogspot.com
Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: a circular reference was detected while serializing an object of type 'System.Reflection.Run...
Oct 29, 2012 02:02 AM|LINK
The reason means that your relationship cannot be serialized into JSON. And for more reason you can refer to something simiar like this:
http://stackoverflow.com/questions/1153385/a-circular-reference-was-detected-while-serializing-an-object-of-type-subsonic
bullrout@gma...
Member
5 Points
49 Posts
Re: a circular reference was detected while serializing an object of type 'System.Reflection.Run...
Oct 29, 2012 10:28 AM|LINK
Hi everyone,
Thanks for answering the question for me. I'm not using MVC for this, I'm using a a generic handler. I'm only needing to return a couple of values not a whole collection. How can I write me code to work and not throw this exception?
Thanks in advance.
Sean
entity data framework Linq
Ashutosh Pat...
Contributor
5737 Points
1105 Posts
Re: a circular reference was detected while serializing an object of type 'System.Reflection.Run...
Oct 29, 2012 11:08 AM|LINK
in such case create a class, make some prooperties and put the values inside from your data source, and return this class from your code.
Blog: http://catchcode.blogspot.com
Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
bullrout@gma...
Member
5 Points
49 Posts
Re: a circular reference was detected while serializing an object of type 'System.Reflection.Run...
Oct 30, 2012 08:47 AM|LINK
Hi There,
The statement returns multiple values, sumed into one. How would I write this if I wanted to return a single value as a decimal?
shopCart.subTotal = (from crtProd in dbContext.tblCartSessions_Products
where crtProd.sessionID == context.Session["id"].ToString()
group crtProd by crtProd.productPrice into g
select new { Numb = g.Sum(p => p.productPrice) });
I get the error:
Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'decimal?'
Any ideas how to get this working?
Sean
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: a circular reference was detected while serializing an object of type 'System.Reflection.Run...
Oct 30, 2012 09:12 AM|LINK
Hello,
You can do this:
shopCart.subTotal = (from crtProd in dbContext.tblCartSessions_Products
where crtProd.sessionID == context.Session["id"].ToString()
group crtProd by crtProd.productPrice into g
select new { Numb = g.Sum(p => p.productPrice) }).FirstOrDefault().Numb;