LINQ,无法加入字符串

Joh*_*ohn 6 .net linq join c#-4.0

我有一个用户列表,每个用户都有问题列表.在我的模型中,问题列表应该是逗号字符串.我尝试:

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; }
}
Run Code Online (Sandbox Code Playgroud)

当然,我得到错误:

LINQ to Entities无法识别方法'System.String Join(System.String,System.String [])'方法,并且此方法无法转换为商店表达式.

如何正确地得到它?

Jon*_*eet 15

我建议在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();
Run Code Online (Sandbox Code Playgroud)