【发布时间】:2016-05-21 15:30:16
【问题描述】:
我无法加入这两个表。
我有这两张桌子
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;
}
【问题讨论】:
-
你有什么尝试吗?你的实体类是什么样子的?您是否将表之间的关系建模为实体类中的导航属性?
标签: c# entity-framework linq