有没有其他方法可以让这个 LINQ 查询工作?
好吧,我有这个: 董事会:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您查看您的
Forum
实体,它已经具有Boards
属性。您应该能够使用Include()
在一个查询中具体化这些内容。或者使用强类型
Include()
:您能否澄清这是代码优先还是数据库优先?从您的示例来看,它看起来像 POCO 模型的数据库优先(代码优先,
Boards
属性应该已经具体化,因为它没有标记为虚拟)。If you look at your
Forum
entity it already has aBoards
property. You should be able to materialize those in one query usingInclude()
.Or using the strongly typed
Include()
: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).这应该可行:
棘手的部分是将板列表分配给
forum.Boards
属性。这不太符合 LINQ 理念:LINQ 是通过某种转换从前一组数据创建下一组数据,而不是修改适当的数据。由于这个原因,这个作业部分变得有点难看。
但是,如果您使用 LINQ-to-entities,它可能有一些更优雅的方法来完成您想要完成的任务。不过,我并不是该技术的专家。
This should work:
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.