Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
Performance 计数性能问题_Performance_Linq_Count - Fatal编程技术网

Performance 计数性能问题

Performance 计数性能问题,performance,linq,count,Performance,Linq,Count,类组如下所示: class Group { public string Name { get; set; } public virtual ICollection<Person> Members { get; set; } public dynamic AsJson() { return new { groupId = this.GroupId, name = this.Name

类组如下所示:

class Group {
    public string Name { get; set; }
    public virtual ICollection<Person> Members { get; set; }

    public dynamic AsJson() {
        return new
        {
            groupId = this.GroupId,
            name = this.Name,
            membersCount = this.Members.Count // issue!
        };
    }
}
            groups = from g in (db.Groups where ... orderby g.Name).ToList()
                     select g.AsJson();
此列表通过返回JSON的AJAX调用检索。该操作的代码如下所示:

class Group {
    public string Name { get; set; }
    public virtual ICollection<Person> Members { get; set; }

    public dynamic AsJson() {
        return new
        {
            groupId = this.GroupId,
            name = this.Name,
            membersCount = this.Members.Count // issue!
        };
    }
}
            groups = from g in (db.Groups where ... orderby g.Name).ToList()
                     select g.AsJson();
问题是这个列表花的时间太长(比如20秒)

我使用维护的属性更改了“this.Members.Count”,即:

class Group {
    public string Name { get; set; }
    public virtual ICollection<Person> Members { get; set; }
    public int MembersCount { get; set; }  // added

    public dynamic AsJson() {
        return new
        {
            groupId = this.GroupId,
            name = this.Name,
            membersCount = this.MembersCount // changed
        };
    }
}
类组{
公共字符串名称{get;set;}
公共虚拟ICollection成员{get;set;}
public int MembersCount{get;set;}//已添加
公共动态AsJson(){
还新
{
groupId=this.groupId,
name=this.name,
MemberScont=this.MemberScont//已更改
};
}
}
它开始工作正常(1-2秒生成列表)


有更好的方法来实现这一点吗?我开始在维护属性MemberScont时遇到问题,因为在代码的几个部分添加和删除了成员

看起来您应该在LINQ查询中急切地加载
成员
。我怀疑在序列化过程中,对于每个
都会触发一个查询来延迟加载其
成员
。这是linq到sql吗?我更新了代码。它是linq到实体和linq到实体的混合体objects@GertArnold您将如何在LINQ查询中加载成员?考虑到我所做的更改,dbGroups.Include(g=>g.Members)。