nishanth yeddula
nishanth yeddula

Reputation: 77

Invalid operation exception when passing JSON string

This is what I have done

        bool query = ( from n in CDC.NCDCPoints
                where n.EVENT_TYPE_ID == et
                where n.BeginDate == b
                where n.EndDate == e
                select n).Count()>0;

       var dupli = (from n in CDC.NCDCPoints
                     where n.EVENT_TYPE_ID == et
                     where n.BeginDate == b
                     where n.EndDate == e
                     select n);
        if (query)
        {
         return new JavaScriptSerializer().Serialize(dupli);
        }
        else
        {
            return "No duplicate";
        }

When I try to convert it into a JSON string, I get a circular reference error. The error occurs at the Serialize step. So, I think probably I get an error because it is an invalid object or something. Do I need to use something like Iqueryable or something. Please help me in getting rid of this error?

Upvotes: 0

Views: 1070

Answers (2)

codeandcloud
codeandcloud

Reputation: 55200

When I ran across the same problem, I did a quick research on why this happens on Entity Framework and came across a wonderful answer at Stack Overflow itself.

Read this: Is this bug ?...got reason behind circular reference ... how to solve but?

You should never serialize the LINQ to SQL (or Entity Framework) classes. Even though Microsoft has placed [DataContract] and other attributes on these classes, they should not be serialized.

Instead, design a set of classes that correctly matches what you want to have serialized.

Try these steps.

  1. Create a mock class called NCDCPointsMock

    public class NCDCPointsMock
    {
        public DateTime? BeginDate { get; set; }
        public DateTime? EndDate { get; set; }
        public int EVENT_TYPE_ID { get; set; }
        // add another dfields you might have in the original NCDCPoints class
        //example        
        //public int ID { get; set; }
    }
    
  2. Now modify your code like this.

    var query = CDC.NCDCPoints
        .Select(n => n.EVENT_TYPE_ID == et 
            && n.BeginDate == b 
            && n.EndDate == e );
    if (query.Any())
    {
        var points = query.ToList();]
        List<NCDCPointsMock> duplicates = new List<NCDCPointsMock>();
        foreach(var point in points)
        {
            NCDCPointsMock dupli = new NCDCPointsMock();
            dupli.ID = point.ID;
            dupli.BeginDate = point.BeginDate;
            dupli.EndDate = point.EndDate;
            dupli.EVENT_TYPE_ID = point.EVENT_TYPE_ID;
            duplicates.Add(dupli);
        }
        return new JavaScriptSerializer().Serialize(dupli);
    }
    else
    {
        return "No duplicate";
    }
    

Or you can try something from here. But that will make the model different from database.

LINQ to SQL and Serialization

Upvotes: 0

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107526

I think this is a little more straightforward. Also, you might need a concrete set of objects in order to serialize them (instead of an IQueryable<T> or IEnumerable<T> that you're getting from the LINQ query, so I threw in a .ToList() to get a List<T>, where T is whatever type is in your NCDCPoints collection. This is completely untested, just so you know.

To avoid the circular reference you mentioned, you can use the technique I added to the LINQ query:

var query = (from n in CDC.NCDCPoints
             where n.EVENT_TYPE_ID == et && n.BeginDate == b && n.EndDate == e
             select new 
             {
                 EventTypeId = n.EVENT_TYPE_ID,
                 BeginDate = n.BeginDate,
                 EndDate = n.EndDate,
                 ... // add the other properties you need on the client side
             });

if (query.Any())
{
    return new JavaScriptSerializer().Serialize(query.ToList());
}
else
{
    return "No duplicate";
}

Upvotes: 1

Related Questions