Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a list of users, each user has list of questions. In my model list of questions should be in string via comma. I try:

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; }
}

of course, I get error:

LINQ to Entities does not recognize the method 'System.String Join(System.String, System.String[])' method, and this method cannot be translated into a store expression.

How to get it correctly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
309 views
Welcome To Ask or Share your Answers For Others

1 Answer

I would suggest doing the string.Join operation locally instead using 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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...