c#linq如何执行此连接?

时间:2016-05-21 15:30:17

标签: c# entity-framework linq

我无法加入这两张桌子。

我有这两个表

SurveySectionTable
-SurveySectionId-(pk)
-surveyId
-SurveySectionName
-comments
-score
...

SurveySectionLoalocazationTable
-SurveySectionLocalizationId-(pk)
-SurveySectionId-(fk)
-SurveySectionName
-comments
...

使用Linq扩展方法我希望根据SurveySectionId加入两个表并获得这样的结果

NewResult
-SurveySectionId
-surveyId(from SurveySectionTable)
-SurveySectionName (from SurveySectionLocalizationTable)
-comments(from SurveySectionLocalizationTable)
-score (from SurveySectionTable)
    ...

我看起来像这样的逻辑,有更好的方法吗?

    public async Task<SurveySectionEditViewModel> GetSurveySectionEditLocalizationVm(Guid surveySectionId, Guid localizationId)
    {   
       var defaultTable = await UOF.SurveySectionService.GetById(surveySectionId);

       var localizationTable = //get by both parameters;

       var result = new SurveySectionEditViewModel
        {
           SurveySectionId = surveySectionId,
           SurveyId = defaultTable.SurveyId,
           SurveySectionName = localizationTable.SurveySectionName,
           Comments = localizatoinTable.Comments,
           Score = defaultTable.Score
        };
        return result;
    }

1 个答案:

答案 0 :(得分:1)

使用SQL创建视图。然后使用EF查询视图。

每当你遇到EF时,正确的答案几乎总是“创建一个视图”。