如何使用LINQ从5个表中进行选择?

时间:2011-08-29 06:25:46

标签: c# linq

我有5张桌子:

course_id | course_name    (course)
------------------------------    
    1     | Basic1
    2     | Basic2
    3     | Basic3
    4     | Basic4
    5     | Basic5

course_id | trainer_id (course_trainer)
-----------------------------
    1     |    1
    1     |    2
    2     |    2
    3     |    2
    4     |    3
    4     |    2
    5     |    3


course_id | topic_id (course_topic)
-----------------------------
    1     |    1
    1     |    2
    2     |    2
    3     |    2
    4     |    3
    4     |    2
    5     |    3

trainer_id| trainer_name (trainer)
-----------------------------
    1     |    Tom
    2     |    Thomas
    3     |    Sue

tropic_id | topic_name (topic)
-----------------------------
    1     |    Skill 1
    2     |    Skill 2
    3     |    Skill 3

如何使用LINQ选择结果如下

Course_name    | Trainer_name     | Topic_name
----------------------------------------------
Basic 1        | Tom, Thomas      | Skill 1, Skill 2
Basic 2        | Thomas           | Skill 2
Basic 3        | Thomas           | Skill 2
Basic 4        | Sue, Thomas      | Skill 3, Skill 2
Basic 5        | Sue              | Skill 3

这是我在C#中的代码,但结果不正确。请帮助我,非常感谢!

public class course_datatable
{
    public string course_name {get; set;}
    public string trainer_name {get; set;}
    public string topic_name {get; set;}
}

IQueryable<course_datatable> coursequery =
    from c in db.course
    join ct in db.course_trainer on c.course_id equals ct.course_id
    join t in db.trainers on ct.trainer_id equals t.trainer_id
    join ctopic in db.course_topic on c.course_id equals ctopic.course_id
    join topic in db.topic on ctopic.topic_id equals topic.topic_id
    select new course_datatable()
    {
        course_name = c.course_name,
        trainer = t.trainer_name,
        topic = topic.topic_name
    };

2 个答案:

答案 0 :(得分:2)

从您的数据库中获取数据:

var result = context.Courses.Select(c => 
    new { Course = c, Trainers = c.Trainers, Skills = c.Skills }).ToList();

然后使用String.Join

展平培训师和技能对象
result.Select(r => new 
{ 
    Course = r.Course.Course_Name, 
    Trainer = String.Join(",", r.Trainers.Select(t => t.TrainerName).ToArray()),
    Skill = String.Join(",", r.Skills.Select(S => S.SkillName).ToArray())
});

修改

使用您的架构,我将重命名,以便它可以正常工作。

var result = db.course.Select(c => new 
{ 
    Course = c, 
    Trainers = c.course_trainer.trainers, 
    Skills = c.course_topic.topic 
}).ToList();

result.Select(r => new 
{ 
    Course = r.Course.course_Name, 
    Trainer = String.Join(",", r.Trainers.Select(t => t.trainer_name).ToArray()),
    Skill = String.Join(",", r.Skills.Select(S => S.topic_name).ToArray())
});

你可以在一个声明中完成所有这些,但我已经用这种方式构建它,以便你有希望更清楚。

答案 1 :(得分:0)

因为您似乎无法使用我的初始答案(这是首选,因为不需要冗余的连接条件),我将使用您现有的代码并向您展示如何分组和项目。

从这开始:

var coursequery =
    from c in db.course
    join ct in db.course_trainer on c.course_id equals ct.course_id
    join t in db.trainers on ct.trainer_id equals t.trainer_id
    join ctopic in db.course_topic on c.course_id equals ctopic.course_id
    join topic in db.topic on ctopic.topic_id equals topic.topic_id
    select new course_datatable()
    {
        course_name = c.course_name,
        trainer = t.trainer_name,
        topic = topic.topic_name
    };

然后,您想要GroupBy course_name

var groups = coursequery.GroupBy(item => item.course_name);

然后每个组都需要投影到新的结果类型

var result = groups.Select(group =>
    new course_datatable
    {
        course_name = group.Key,
        trainer_name = String.Join(",", group.Select(i=> i.trainer_name).ToArray()),
        topic_name = String.Join(",", group.Select(i => i.topic_name).ToArray()),
    }).ToList();

或者如果你想尝试别的东西(为了好玩)使用LINQs Aggregate方法,很少使用:

var result = groups.Select(group =>
    group.Aggregate((initial, next) =>
    {
        initial.topic_name += String.Format(", {0}", next.topic_name);
        initial.trainer_name += String.Format(", {0}", next.trainer_name);
        return initial;
    })).ToList();