Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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# 实体框架将子实体的获取计数作为属性_C#_Sql_Entity Framework_Entity Framework 6 - Fatal编程技术网

C# 实体框架将子实体的获取计数作为属性

C# 实体框架将子实体的获取计数作为属性,c#,sql,entity-framework,entity-framework-6,C#,Sql,Entity Framework,Entity Framework 6,我有两个实体,分别描述Post和Comments public class Post { public int Id { get; set; } public ICollection<Comment> Comments { get; set; } //other properties //Unmapped property public int NumberOfComments { get; set; } } public clas

我有两个实体,分别描述
Post
Comments

public class Post
{
    public int Id { get; set; }
    public ICollection<Comment> Comments { get; set; }

    //other properties 


    //Unmapped property
    public int NumberOfComments { get; set; }

}

public class Comment
{
    public int Id { get; set; }
    public Post Post { get; set; }
    public int PostId { get; set; }

    // other properties
}
公共类职位
{
公共int Id{get;set;}
公共ICollection注释{get;set;}
//其他属性
//未映射属性
public int NumberOfComments{get;set;}
}
公开课评论
{
公共int Id{get;set;}
公共Post Post{get;set;}
公共int PostId{get;set;}
//其他属性
}
现在我想让
NumberOfComments
属性填充帖子的实际评论数

  • 尝试将查询结果投影到帖子集合中 模特没有锻炼

  • 尝试加入表,然后按帖子id分组,但仍然没有成功 似乎有效


我不能简单地
返回p.Comments.Count作为属性定义,因为我在查询过程中不包括注释。我只需要注释的计数,而不是内存中的整个集合。

您可以使用
.count()
获取注释的数量

public class Post
{
    public int Id{ get; set; }
    public ICollection<Comment> Comments { get; set; }

    //other properties 

    //Unmapped property
    public int NumberOfComments { get { return Comments.Count(); } }

}
公共类职位
{
公共int Id{get;set;}
公共ICollection注释{get;set;}
//其他属性
//未映射属性
public int NumberOfComments{get{return Comments.Count();}
}

如果始终检索注释导航,则可以执行以下操作:

public class Post
{
    public int Id { get; set; }
    public ICollection<Comment> Comments { get; set; }

    //other properties 

    //Unmapped property: Since it's value depends on the Comments collection
    //we don't need to define the setter. If the collection is null, it'll
    //return zero.
    public int NumberOfComments
    {
        get
        {
            return this.Comments?.Count() ?? 0;
        }
    }
}
公共类职位
{
公共int Id{get;set;}
公共ICollection注释{get;set;}
//其他属性
//未映射属性:因为它的值取决于注释集合
//我们不需要定义setter。如果集合为null,它将
//返回零。
公众意见
{
得到
{
返回此.Comments?.Count()??0;
}
}
}
关于这一行<代码>返回此.Comments?.Count()??0;

这里我们使用两个空操作符,空条件操作符
和空合并操作符


第一种方法通过在调用
.Count()
之前立即返回null值来避免代码在Comments属性为null时引发错误,第二种方法在左侧表达式为null时运行,因此如果
this.Comments?.Count()
返回null,它将为您提供0。

这是我发现的方法

以下课程是必要的:

public class PostExtraData
{
    public Post post { get; set; }
    public int NumberOfComments { get; set; } 
}

public class Post
{
    public int Id { get; set; }
    public ICollection<Comment> Comments { get; set; }

    //other properties 

}

public class Comment
{
    public int Id { get; set; }
    public Post Post { get; set; }
    public int PostId { get; set; }

    // other properties
}

public int NumberOfComments{get{return Comments.Count();}}
?我没有包括注释。我只想得到评论的数量。我想你必须在你的查询中明确地请求它
选择新帖子{Id=p.Id,NumberOfComments=p.comments.Count()}
?juharr谢谢。但它会抛出“实体无法在LINQtoEntities查询中构造”@Aneef只需执行类似于
。。。选择new{Id=p.Id,NumberOfComments=p.Comments.Count()}).AsEnumerable().select(p=>newpost{Id=p.Id,NumberOfComments=p.NumberOfComments})
。或者考虑为您要使用的特定数据创建单独的DTO,并将其他层与实体分开。我不包括注释。我只想得到评论的数量。你不包括评论是什么意思。它们是:public ICollection Comments{get;set;}-这是您的代码,不是吗?@AlexeiFimine在EF中,它并不总是提取所有相关表的所有数据,这些表表示为导航属性。我在查询中不使用db.Posts.Include(p=>p.Comments)。所以评论不会被正确检索。所以comments.count会在这里抛出一个错误??请参阅下面我的答案。它将始终为0.bcoz我不总是检索评论请在Matts答案中查看我的回复和juharrs的回复。我知道这个??操作员和导航属性。但是,如果您没有在每个帖子中检索注释,那么您可以在另一个查询中单独进行计数(您必须将setter返回NumberOfComments属性)。myPost.NumberOfComments=.db.Comments.Where(c=>c.PostId==PostId.Count();阿伦堡。我认为这不是一种有效的方法。至于生成的查询,我认为它将在数据库中使用COUNT()sql函数,这样以后就不会将数据检索到对象并在内存中计数@阿内夫
return _context.Post
       .Select(p => new PostExtraData
       {
           Post= p,
           NumberOfComments = p.Comments.Count(),
       })
       .ToList();