查询模型内部,如何获取特定类中的项目数

时间:2011-05-10 12:24:53

标签: entity-framework entity-framework-4

我想出了这个:

[NotMapped]
    public int ThreadsInBoard
    {
        get
        {
            ForumContextContainer ctx = new ForumContextContainer();
            int thr = (from p in ctx.BoardSet
                       from x in ctx.ThreadSet
                       where p.BoardID == x.Board.BoardID
                       select p).Count();
        }
    }

我想要做的是特定电路板中的线程数。 此查询实际上返回数据库中每个可能线程的计数并分配它。 以下是涉及的课程

    public partial class Board
{
    public int BoardID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public bool IsVisibleToGuests { get; set; }
    public bool IsLocked { get; set; }

    public Forum Forum { get; set; }
    public ICollection<Thread> Thread { get; set; }
}

    public partial class Thread
{
    public int ThreadID { get; set; }

    public User User { get; set; }
    public ICollection<Post> Post { get; set; }
    public Board Board { get; set; }
    public ICollection<Subscription> Subscription { get; set; }
    public ICollection<Poll> Poll { get; set; }
}

1 个答案:

答案 0 :(得分:1)

[NotMapped]
    public int ThreadsInBoard
    {
        get
        {
            ForumContextContainer ctx = new ForumContextContainer();
            int thr = ctx.ThreadSet.Count(p => p.BoardID == this.BoardID);
        }
    }

假设此类具有boardId属性