Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 传递JSON字符串时出现无效操作异常_C#_Jquery_Json - Fatal编程技术网

C# 传递JSON字符串时出现无效操作异常

C# 传递JSON字符串时出现无效操作异常,c#,jquery,json,C#,Jquery,Json,这就是我所做的 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

这就是我所做的

        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";
        }

当我尝试将其转换为JSON字符串时,会出现循环引用错误。该错误发生在序列化步骤。所以,我想可能是因为它是一个无效的对象或什么的,所以我得到了一个错误。我需要使用Iqueryable之类的东西吗。请帮我摆脱这个错误

我认为这更直接一点。此外,您可能需要一组具体的对象来序列化它们(而不是从LINQ查询中获得的
IQueryable
IEnumerable
,因此我加入了
.ToList()
获取
列表,其中
T
NCDCPoints
集合中的任何类型。正如您所知,这是完全未经测试的

为了避免您提到的循环引用,您可以使用我添加到LINQ查询中的技术:

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";
}

当我遇到同样的问题时,我快速研究了为什么会在实体框架上发生这种情况,并在堆栈溢出本身上找到了一个极好的答案

请阅读以下内容:

永远不要将LINQ序列化为 SQL(或实体框架)类。 尽管微软已经 [DataContract]和上的其他属性 这些类,它们不应该是 连载

相反,设计一组 正确匹配您想要的内容 已序列化

试试这些步骤

  • 创建一个名为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; }
    }
    
  • 现在像这样修改代码

    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";
    }
    
    var query=CDC.NCDCPoints
    .Select(n=>n.EVENT\u TYPE\u ID==et
    &&n.BeginDate==b
    &&n.EndDate==e);
    if(query.Any())
    {
    var points=query.ToList();]
    列表重复项=新列表();
    foreach(变量点到点)
    {
    NCDCPointsMock dupli=新的NCDCPointsMock();
    dupli.ID=point.ID;
    dupli.BeginDate=point.BeginDate;
    dupli.EndDate=point.EndDate;
    dupli.EVENT\u TYPE\u ID=point.EVENT\u TYPE\u ID;
    副本。添加(dupli);
    }
    返回新的JavaScriptSerializer().Serialize(dupli);
    }
    其他的
    {
    返回“无重复”;
    }
    
  • 或者你可以从这里尝试一些东西,但这会使模型与数据库不同


    离题:使用
    .Any()
    扩展而不是
    Count()>0
    任何原因你有多个
    where
    s而不是仅仅使用
    &
    ,或者在使用它的范围之外定义
    dupli
    ?dupli可以在内部定义,如果….让我试试那个..@Bala:Ya可以使用Any()…我会用that@Cory:没有用,我仍然得到相同的错误。@Cory:我仍然得到相同的循环引用错误(这是某种类型的无效操作异常)..
    JavascriptSerializer
    使用反射来序列化列表,但您必须有一个指向
    CDC.NCDCPoints
    列表的属性。如果集合具有循环引用,则无法序列化。@Carlson:噢……那么您能告诉我另一种方法吗?好的……我有50个字段在其中。那么,我还有其他方法吗继续这个。嘿……我的问题是,我的NCDCPoints中有这么多字段。它们是动态的。所以,除了序列化,我还有其他方法吗?@nishanth yeddula:事实上,如果我之前看到cory的答案,我就不会发布这个了。方法略有不同,但实际上是相同的。