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

发布于 2024-11-06 17:14:35 字数 1137 浏览 0 评论 0原文

我想出了这个:

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

I've come up with this:

[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();
        }
    }

What I want to do is the count of threads, that are in the specific board.
This query actually return count of every possible thread in database and assign it.
Below are involved classes

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-11-13 17:14:35
[NotMapped]
    public int ThreadsInBoard
    {
        get
        {
            ForumContextContainer ctx = new ForumContextContainer();
            int thr = ctx.ThreadSet.Count(p => p.BoardID == this.BoardID);
        }
    }

假设此类具有 boardId 属性

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

assuming this class has the boardId property

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文