如何将LINQ方法语法转换为LINQ查询语法

时间:2016-12-19 06:58:52

标签: c# entity-framework linq

如何将以下LINQ方法语法转换为LINQ查询语法?有没有一个工具可以帮助做到这一点?我知道LINQPAD可以从Query语法转换为Method语法,但反之亦然。我想将它转换为查询语法的原因是对模型进行一些投影,以便我可以从LINQ查询中提到的所有表中选择几列。

var queryResult = CurrentContext.Set<AM_Response_T>().Include(t => t.AM_Master_T)
        .Include(t => t.AM_Supporting_Evidences_T)
        .Include("AM_Supporting_Evidences_T.Ticket_Repository_T")
        .Include(t => t.AM_Response_Validation_T)
        .Include("AM_Response_Validation_T.Amazon_Users_T")
        .Include("AM_Response_Validation_T.Amazon_Users_T1")
        .Include("AM_Response_Validation_T.AM_Validator_Type_T")
        .Include(t => t.Catalog_Type_T)
        .Include(t => t.Skills_T)
        .Include("Skills_T.Record_Status_T")
        .Include("Skills_T.BE_PS_Skill_Map_T")
        .Include("Skills_T.BE_PS_Skill_Map_T.Skills_T1")
        .Include("Skills_T.BE_PS_Skill_Map_T.Skills_T1.Record_Status_T")
        .Include("Skills_T.BE_PS_Skill_Map_T.Skills_T1.BE_PS_Skill_Map_T")
        .Include("Skills_T.BE_PS_Skill_Map_T.Skills_T1.KE_PS_Skill_Map_T")
        .Include("Skills_T.KE_PS_Skill_Map_T")
        .Include("Skills_T.KE_PS_Skill_Map_T.Skills_T1")
        .Include("Skills_T.KE_PS_Skill_Map_T.Skills_T1.Record_Status_T")
        .Include("Skills_T.KE_PS_Skill_Map_T.Skills_T1.BE_PS_Skill_Map_T")
        .Include("Skills_T.KE_PS_Skill_Map_T.Skills_T1.KE_PS_Skill_Map_T")
        .Include(t => t.Skill_Levels_T)
        .Include(t => t.Skills_T.Skill_Levels_T)
        .Include(t => t.Questions_T)
        .Include(t => t.Questions_T.Question_Type_T)
        .Include(t => t.AM_Master_T.AM_Master_Member_Map_T)
        .Include(t => t.AM_Master_T.AM_Master_SPR_Map_T)
        .Include(t => t.AM_Master_T.AM_Master_FSPR_Map_T)
        .Include(t => t.AM_Master_T.AM_Master_FRP_Map_T)
        .Include(t => t.AM_Master_T.AM_Master_Org_Function_Map_T)
        .Include(t => t.AM_Master_T.AM_Master_RRP_Map_T)
        .Include(t => t.AM_Master_T.AM_Master_Skills_Map_T)
        .Include(t => t.AM_Master_T.AM_Master_FCM_Position_T)
        .Include("AM_Master_T.AM_Master_FCM_Position_T.FCM_Position_T")
        .Include(t => t.Amazon_Users_T)
        .Include(t => t.Amazon_Users_T1)
        .Include(t => t.AM_Response_Activity_T);

1 个答案:

答案 0 :(得分:0)

您不需要使用查询语法,方法语法可以正常工作。您可以在那里使用.Select()和.ToList()并投影到模型:

List<ModelYouWantToProjectTo> queryResult = CurrentContext.Set<AM_Response_T>()
    .Include(t => t.AM_Master_T)
    .Include(t => t.AM_Supporting_Evidences_T)
    // Skipped a few
    .Include(t => t.AM_Response_Activity_T)
    .Select(t => new ModelYouWantToProjectTo
        {
            ModelProperty = t.Property,
            // Etc...
        })
     .ToList();

据我所知,查询语法中没有等效的.Include()。你可以混合使用这两种语法,但我发现它并没有清楚地读出来。