Dot-Net
LINQ,無法加入字元串
我有一個使用者列表,每個使用者都有問題列表。在我的模型中,問題列表應該通過逗號以字元串形式出現。我嘗試:
public List<ITW2012Mobile.ViewModels.AdminSurveyReportModel> SurveyReportList() { var q = from i in _dbContext.Users where i.UserId != null select new ITW2012Mobile.ViewModels.AdminSurveyReportModel() { FirstName = i.FirstName, LastName = i.LastName, Question4 = String.Join(", " , (from a in _dbContext.MultipleQuestions where a.MultipleQuestionType.KEY == MultipleQuestionKeys.BENEFITS select a.Question).ToArray()) }; return q.ToList(); } public class AdminSurveyReportModel { public string FirstName { get; set; } public string LastName { get; set; } public string Question4 { get; set; } }當然,我得到錯誤:
LINQ to Entities 無法辨識方法 ‘System.String Join(System.String, System.String
$$ $$)’ 方法,並且該方法不能翻譯成商店表達式。
如何正確獲取?
我建議在
string.Join本地進行操作,而不是使用AsEnumerable:var q = from i in _dbContext.Users where i.UserId != null select new { FirstName = i.FirstName, LastName = i.LastName, Question4Parts = _dbContext.MultipleQuestions .Where(a => a.MultipleQuestionType.KEY == MultipleQuestionKeys.BENEFITS) .Select(a => a.Question) }; return q.AsEnumerable() .Select(x => new ITW2012Mobile.ViewModels.AdminSurveyReportModel { FirstName = x.FirstName, LastName = x.LastName, Question4 = string.Join(", ", x.Question4Parts) }) .ToList();