有没有其他方法可以让这个 LINQ 查询工作?

发布于 2024-11-05 18:43:49 字数 1545 浏览 0 评论 0原文

好吧,我有这个: 董事会:

    public class Board
{
    public int BoardID { get; set; }
    public string BoardName { get; set; }
    public string BoardDescription { get; set; }
    public int PostCount { get; set; }
    public int TopicCount { get; set; }
    public bool IsVisibile { get; set; }
    public Forum BelongToForum { get; set; }
    public List<Board> Boards { get; set; }
    public List<Topic> Topics { get; set; }
}

和论坛:

    public class Forum
{
    public int ForumID { get; set; }
    public string ForumName { get; set; }
    public string ForumDescription { get; set; }
    public List<Board> Boards { get; set; }
}

以及通过董事会获取论坛的方法:

        public List<Forum> GetForums()
    {
        List<Forum> forums = new List<Forum>();
        _ctx = new ForumContext();
        forums = (from p in _ctx.Forums
                  select p).ToList();
        List<Forum> returnForm = new List<Forum>();
        foreach(Forum forum in forums)
        {
            List<Board> board = (from x in _ctx.Boards
                                 where x.BelongToForum.ForumID == forum.ForumID
                                 select x).ToList();
            forum.Boards = board;

            returnForm.Add(forum);
        }


        return returnForm;
    }

它按预期工作。但正如你所看到的,我使用了三个列表和 foreach 循环。对我来说看起来不太好。所以我有一个问题,还有其他方法可以让它发挥作用吗?

我想我应该创建另一个模型类,它将从连接或类似的东西收集必要的数据。 但我想问的是还有其他方法吗?

Well I have this:
Board:

    public class Board
{
    public int BoardID { get; set; }
    public string BoardName { get; set; }
    public string BoardDescription { get; set; }
    public int PostCount { get; set; }
    public int TopicCount { get; set; }
    public bool IsVisibile { get; set; }
    public Forum BelongToForum { get; set; }
    public List<Board> Boards { get; set; }
    public List<Topic> Topics { get; set; }
}

And Forum:

    public class Forum
{
    public int ForumID { get; set; }
    public string ForumName { get; set; }
    public string ForumDescription { get; set; }
    public List<Board> Boards { get; set; }
}

And method that get Forums with boards:

        public List<Forum> GetForums()
    {
        List<Forum> forums = new List<Forum>();
        _ctx = new ForumContext();
        forums = (from p in _ctx.Forums
                  select p).ToList();
        List<Forum> returnForm = new List<Forum>();
        foreach(Forum forum in forums)
        {
            List<Board> board = (from x in _ctx.Boards
                                 where x.BelongToForum.ForumID == forum.ForumID
                                 select x).ToList();
            forum.Boards = board;

            returnForm.Add(forum);
        }


        return returnForm;
    }

It's working as expected. But as you see I'm using three lists, and foreach loop. It doesn't look really good for me. So I have an question, is there another way to make it work ?

I think I should made another model class that will gather nessesary data from join or something like this.
But what I want to ask are there other ways to do it ?

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

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

发布评论

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

评论(2

春夜浅 2024-11-12 18:43:49

如果您查看您的 Forum 实体,它已经具有 Boards 属性。您应该能够使用 Include() 在一个查询中具体化这些内容。

 forums = (from p in _ctx.Forums.Include("Boards")
           select p).ToList();

或者使用强类型 Include()

 forums = (from p in _ctx.Forums.Include( x=> x.Boards)
           select p).ToList();

您能否澄清这是代码优先还是数据库优先?从您的示例来看,它看起来像 POCO 模型的数据库优先(代码优先, Boards 属性应该已经具体化,因为它没有标记为虚拟)。

If you look at your Forum entity it already has a Boards property. You should be able to materialize those in one query using Include().

 forums = (from p in _ctx.Forums.Include("Boards")
           select p).ToList();

Or using the strongly typed Include():

 forums = (from p in _ctx.Forums.Include( x=> x.Boards)
           select p).ToList();

Can you clarify whether this is code first or DB first? From your example it looks like DB first with POCO models (with code first the Boards property should have been materialized already since it is not marked as virtual).

可爱咩 2024-11-12 18:43:49

这应该可行:

var returnForm =
    (
        from forum in _ctx.Forums
        join b in _ctx.Boards on forum.ForumID equals b.BelongsToForum.ForumID into boards
        select new { forum, boards }
    )
    .Select( x =>
        {
            x.forum.Boards = x.boards.ToList();
            return x.forum;
        } )
    .ToList();

棘手的部分是将板列表分配给 forum.Boards 属性。这不太符合 LINQ 理念:LINQ 是通过某种转换从前一组数据创建下一组数据,而不是修改适当的数据。

由于这个原因,这个作业部分变得有点难看。

但是,如果您使用 LINQ-to-entities,它可能有一些更优雅的方法来完成您想要完成的任务。不过,我并不是该技术的专家。

This should work:

var returnForm =
    (
        from forum in _ctx.Forums
        join b in _ctx.Boards on forum.ForumID equals b.BelongsToForum.ForumID into boards
        select new { forum, boards }
    )
    .Select( x =>
        {
            x.forum.Boards = x.boards.ToList();
            return x.forum;
        } )
    .ToList();

The tricky part is with assigning the list of boards to the forum.Boards property. This doesn't quite fit the LINQ philosophy: LINQ is about creating next set of data from previous set of data via some kind of transformation, not about modifying the data in place.

For this reason, this assignment part came out a bit ugly.

However, if you're using LINQ-to-entities, it probably has some more elegant ways to accomplish what you're trying to accomplish. I'm not a big pro in that technology, though.

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