如何编写EF查询以连接三个表并按字段排序?

时间:2017-03-17 22:58:34

标签: c# mysql entity-framework

我有以下3个表格,如下所示。我试图在3个表中编写实体框架连接查询 到

Select joblink, usersubmitted, runstatus, submitted_time, changelist 
orderby submitted time

我能够从2个表中加入和检索数据,但无法弄清楚 关于如何加入3个表,任何人都可以提供如何做到这一点的指导吗?

lookahead_run (Table#1) 
+-------------------+--------+----------------+-------------------+----------+
lookahead_run_id(PK)|joblink |  usersubmitted |submitted_time     |runstatus
+-------------------+--------+----------------+-------------------+----------+
15963---------------+link1---+---username1----+2017-03-17 22:28:53--Fail-----


lookahead_run_change_list (Table#2)
+---------------+----------------+-----------------+
changelistid(PK)|lookahead_run_id|change_list_id
+---------------+----------------+-----------------+
38591-----------+15963-----------+34022
38590-----------+15963-----------+34021
38589-----------+15963-----------+34020


change_lists (Table#3)
+-------------+-----------+
change_list_id|changelist
+-------------+-----------+
34022--------  1823900
34021--------  1819483
34020--------  1818572

更新的代码; -

namespace Dashboard.Model.ApiModels
{
    public class LookaheadRunInfo
    {
        public string ECJobLink { get; set; }
        public List<String> gerrits { get; set; }
        public string UserSubmitted { get; set; }
        public string SubmittedTime { get; set; }
        public string RunStatus { get; set; }
    }
}

        public IEnumerable<LookaheadRunInfo> GetLookaheadRunInfoSearch(LookaheadRunsFilterCriteria filterCriteria)
        {
            List<LookaheadRunInfo> lookaheadRunsInfo = new List<LookaheadRunInfo>();           
            var lookaheadRunData = bitDB.lookahead_run.OrderBy(x => x.lookahead_run_id).Skip(filterCriteria.PageNumber * filterCriteria.PageSize).Take(filterCriteria.PageSize).ToList();
            foreach (var lookaheadRunRow in lookaheadRunData)
            {
                var lookaheadRunId = lookaheadRunRow.lookahead_run_id;
                 lookaheadRunsInfo = (from lrcl in bitDB.lookahead_run_change_list
                                                 join cl in bitDB.change_lists on lrcl.change_list_id equals cl.change_list_id
                                                 join lr in bitDB.lookahead_run on lrcl.lookahead_run_id equals lr.lookahead_run_id
                                                 where lrcl.lookahead_run_id == lookaheadRunId
                                                 orderby lr.submission_time
                                                 select new LookaheadRunInfo
                                                 {
                                                     lr.ec_job_link,
                                                     cl.change_requests,
                                                     lr.submitted_by,
                                                     lr.submission_time,
                                                     lr.lookahead_run_status,

                                                 }).ToList();
            }
            return lookaheadRunsInfo;
        }

错误: -

Error   1   Cannot initialize type 'Dashboard.Model.ApiModels.LookaheadRunInfo' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

1 个答案:

答案 0 :(得分:0)

您可以在查询中添加任意数量的联接。如果要从多个实体中选择字段,则需要选择一个新对象。我建议你创建一个包含连接内容的持有者类,但我会告诉你如何使用匿名类型:

var lookaheadRunChangeListIds = (from lrcl in bitDB.lookahead_run_change_list
    join cl in bitDB.change_lists on lrcl.change_list_id equals cl.change_list_id
    join lr in bitDB.lookahead_run on lrcl.lookahead_run_id equals lr.lookahead_run_id
    where lrcl.lookahead_run_id == lookaheadRunId
    orderby lr.submitted_time
    select new LookaheadRunInfo { 
        ECJobLink = lr.joblink, 
        UserSubmitted = lr.usersubmitted, 
        RunStatus = lr.runstatus, 
        SubmittedTime = lr.submitted_time, 
        gerrits = cl.changelist 
    }).ToList();

请注意,new { ... }可以替换为new NewClass { ... },其中NewClass包含所有选定的字段。

编辑上方:在您设置LookAheadRun时为您的属性命名,因为名称不同,您应该映射它们。