C# 如何迭代LINQ到SQL的几个左连接

C# 如何迭代LINQ到SQL的几个左连接,c#,linq,asp.net-mvc-3,C#,Linq,Asp.net Mvc 3,当我尝试循环使用这个SQL到LINQ方法时,我只接收forum_类别的所有行。我真正希望它做的是从forum_category中获取所有行,并返回与类别id匹配的所有论坛,以下是预期结果: FORUM_CATEGORY (categoryid, categorytitle) - FORUM (forumid, forumtitle, forumdescrition) -> (latest topic => topicid, topictitle) -> (latest pos

当我尝试循环使用这个SQL到LINQ方法时,我只接收forum_类别的所有行。我真正希望它做的是从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);
这只是我所做的一些不同方法的一个例子


编辑:更明确地阐明了我想要什么。

我认为您的排序导致了问题,但经过进一步审查

我有另一个想法,你的ForumIndex类看起来不符合你的要求。你的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...

    }
类为umindexmodel
{
int CategoryId{get;set;}
字符串CategoryTitle{get;set;}
列表对象{get;set;}
列表标题{get;set;}
列表说明{get;set;}
…你明白了。。。
}
要选择进入列表,请执行以下操作:

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();
公共C类
{
A{get;set;}
列出b{get;set;}
}
//g是一个IEnumerable,其键属性为a
列出结果=
(
来自ListA的一家公司
将a.ValueAB=b.ValueAB上的列表b中的b连接到g中
选择新的C()
{
TheA=g.键,
TheBs=g.ToList()
}
).ToList();

我将根据原始海报的要求添加另一个答案。我使用的是实体框架,并定义了示例中使用的关联。我的数据模型(出于本例的目的)是三个表,因为存在多对多关系,但如果只有一对多关系的类别表和论坛表(加上addtl.look-up表),则您的数据模型会更简单

所以我的数据模型(物理)

  • lm_m_类别
  • lm_m_category_链接(这是多对多表)
  • lm_m_链接
我在EF中定义了关联(nCatnLink,用于从外部参照表导航到类别或链接)。EF以这种方式为您处理连接

因此,加载该语句的代码不是一条语句,而是您让我向您展示如何使用EF来完成它。我没有举例说明Category和Link类,它们是简单的id,desc类,但是Category有一个链接列表。我的物理数据模型使用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);
}
列表类别=新列表();
新类别;
链接新建链接;
foreach(db.lm_m_category_link.Include(“nCategory”).Include(“nLink”).ToList()中的var类别)
{
newCategory=新类别();
newCategory.category\u id=category.category\u id;
newCategory.category\u name=category.nCat.category\u name;
foreach(在(IEnumerable)category.nLink中的变量链接)
{
newLink=新链接();
newLink.link\u id=link.link\u id;
newLink.link\u name=link.link\u title;
newLink.link\u url=link.link\u url;
//添加链接到列表
newCategory.category\u links.Add(newLink);
}
//添加类别
类别。添加(新类别);
}

您想要一个带有(1)posted和PostCount、TopicCount的结果吗?一篇文章可以属于多个主题吗?一篇文章不能属于多个主题,为什么会呢?我不知道。为什么你有一个带有PostId和TopicCount的类?你的ForumIndexModel类每个类别只有一个论坛。你不应该有一个列表,列表,或者更好的使用论坛类和使用列表吗?有些事情看起来不对劲,这正是我想要做的。在选择新的ForumIndexModel时,我将如何设置这些列表?我获得了正确的数据,更改排序没有任何区别。然而,我只能循环一次我的论坛数据,在每个类别上循环正确。当我选择进入ForumIndexModel时,如何通过LINQ分配这些列表?好的,看来您在选择之后正在通过ForumIndexModel循环。。。这意味着我在这里的第二个答案也可能是错误的,我的缺点是没有得到澄清。我通常使用实体框架,不知道如何使用Linq在一个语句中分配列表,如果可能的话。我希望看到linq为该查询生成的SQL,因为看起来您的查询没有问题。我还使用实体框架。但是,如果我不能用一句话来表达,我怎么能用其他方式表达呢?虽然这不是我一直在寻找的正确答案,但这帮助我走上了正确的道路。