当我尝试将此SQL循环到LINQ方法时,我只接收了forum_category的所有行。我真正希望它做的是从forum_category获取所有行,并返回所有与类别ID匹配的论坛,这是预期的结果:
FORUM_CATEGORY (categoryid, categorytitle)
- FORUM (forumid, forumtitle, forumdescrition) -> (latest topic => topicid, topictitle) -> (latest post on that latest topic => postid, postadded, username) -> total topic count, total post count in this forum only
- FORUM (forumid, forumtitle, forumdescrition) -> (latest topic => topicid, topictitle) -> (latest post on that latest topic => postid, postadded, username) -> total topic count, total post count in this forum only
- FORUM (forumid, forumtitle, forumdescrition) -> (latest topic => topicid, topictitle) -> (latest post on that latest topic => postid, postadded, username) -> total topic count, total post count in this forum only
FORUM_CATEGORY
- FORUM [...]
- FORUM [...]
- FORUM [...]
你明白了......
-
这是我目前得到的:
FORUM_CATEGORY
- FORUM (forumid, forumtitle, forumdescrition) -> (latest topic => topicid, topictitle) -> (latest post on that latest topic => postid, postadded, username) -> total topic count, total post count in this forum only
FORUM_CATEGORY
- FORUM (forumid, forumtitle, forumdescrition) -> (latest topic => topicid, topictitle) -> (latest post on that latest topic => postid, postadded, username) -> total topic count, total post count in this forum only
也就是说,每个论坛类别只有一个论坛。 这是LINQ代码:
var forum = (from c in context.forum_category
join f in context.forum on c.id equals f.categoryid
join t in context.forum_topic on f.id equals t.forumid
join tc in context.forum_topic on f.id equals tc.forumid into tcount
join p in context.forum_posts on t.id equals p.topicid
join pc in context.forum_posts on t.id equals pc.topicid into pcount
join u in context.users on p.userid equals u.id
orderby p.added descending
select new ForumIndexModel
{
CategoryId = c.id,
CategoryTitle = c.title,
ForumId = f.id,
ForumTitle = f.title,
ForumDescription = f.description,
TopicId = t.id,
TopicTitle = t.title,
PostId = p.id,
PostAdded = p.added,
Username = u.username,
TopicCount = tcount.Count(),
PostCount = pcount.Count()
}).ToList();
return View(forum);
这只是我所做的一些不同方法的一个例子。
编辑:澄清我想要的更具体的内容。
答案 0 :(得分:1)
我将根据原始海报的要求添加另一个答案。我正在使用实体框架,并且在我的示例中使用了已定义的关联。我的数据模型(出于本示例的目的)是三个表,因为存在多对多的关系,但是只有一对多关系的类别和论坛表(加上addtl。查找表)会更简单。
所以我的数据模型(物理)
我在EF中定义了关联( nCat , nLink 用于导航到类别或从外部参照表链接)。 EF以这种方式处理连接。
所以加载它的代码不是一个声明,但是你让我告诉你如何使用EF来实现它。我没有把Category和Link类的例子放在简单的id,desc类但是Category有一个Links列表。我的物理数据模型使用lm_m_ *表。
List<Category> Categories = new List<Category>();
Category newCategory;
Link newLink;
foreach (var category in db.lm_m_category_link.Include("nCategory").Include("nLink").ToList())
{
newCategory = new Category();
newCategory.category_id = category.category_id;
newCategory.category_name = category.nCategory.nCat.category_name;
foreach (var link in (IEnumerable<lm_m_link>)category.nLink)
{
newLink = new Link();
newLink.link_id = link.link_id;
newLink.link_name = link.link_title;
newLink.link_url = link.link_url;
// add link to list
newCategory.category_links.Add(newLink);
}
// add category
Categories.Add(newCategory);
}
答案 1 :(得分:0)
我虽然你的排序引起了问题,但经过进一步的审查......
我还有另外一个想法,你的 ForumIndexClass看错了,你想要什么。您的ForumIndexModel不应该在论坛列上有列表(或IEnumerable)吗?您正在分配应该有列表的单个值。你的课看起来应该更像......
class ForumIndexModel
{
int CategoryId {get; set;}
string CategoryTitle {get; set;}
List<int> ForumIds {get; set;}
List<string> ForumTitles {get; set;}
List<string> ForumDescriptions {get; set;}
... you get the idea...
}
要选择列表,请执行以下操作:
public class C
{
A TheA {get;set;}
List<B> TheBs {get;set;}
}
//g is an IEnumerable<B> with a key property of A
List theResult =
(
from a in ListA
join b in ListB on a.ValueAB = b.ValueAB into g
select new C()
{
TheA = g.Key,
TheBs = g.ToList()
}
).ToList();