LINQ, can't join to string

后端 未结 3 540
暖寄归人
暖寄归人 2020-11-30 12:41

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

        
相关标签:
3条回答
  • 2020-11-30 13:19

    can't include the string.Join() in the initial projection, because the LINQ translator doesn't support it. Can I write a custom translator for it?

    0 讨论(0)
  • 2020-11-30 13:20

    Try using the Aggregate method.

    Question4 = (from a in _dbContext.MultipleQuestions where a.MultipleQuestionType.KEY == MultipleQuestionKeys.BENEFITS select a.Question).ToArray().Aggregate((x,y) => x + "," + y)
    

    Have not tested

    0 讨论(0)
  • 2020-11-30 13:21

    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();
    
    0 讨论(0)
提交回复
热议问题