0

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?

5
  • off topic: use .Any() extension instead of Count() > 0 Commented Jun 16, 2011 at 4:07
  • Any reason you have multiple wheres instead of just using &&, or defining dupli outside of the scope in which you use it? Commented Jun 16, 2011 at 4:10
  • dupli can be defined inside if.... Let me try that one.. Commented Jun 16, 2011 at 4:12
  • @Bala: Ya can use Any()... I'll use that Commented Jun 16, 2011 at 4:13
  • @Cory: No use, I still get the same error. Commented Jun 16, 2011 at 4:15

2 Answers 2

1

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";
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Cory: I still get the same error of circular reference(which is some type invalid operation exception)..
JavascriptSerializer uses reflection to serialize your list, but you must have a property that points back to the CDC.NCDCPoints list. A collection can't be serialized if it has circular references.
@Carlson: Oh... So Can u please tell me another way to do this?
K... I have 50 fields in there. So, Do i have any other way to proceed with this one.
0

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

2 Comments

hey..My problem is that I have so many fields in the NCDCPoints. And they are dynamic. So, apart from serialization , do i have any other way of doing this?
@nishanth yeddula: actually i wouldn't have posted this if i had seen cory's answer before hand. slightly different approach but in effect the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.