LINQ,不能加入到字符串 [英] LINQ, can't join to string

查看:249
本文介绍了LINQ,不能加入到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

予有用户的列表,每个用户都有问题列表。在我的问题型号列表应该是在字符串通过逗号。我尝试:

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到实体不能识别方法System.String   加入(System.String,System.String [])'方法,和这种方法不能   被翻译成存储前pression

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.

如何正确地得到它?

推荐答案

我建议做了的string.join 运行在本地,而不是使用 AsEnumerable

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();

这篇关于LINQ,不能加入到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆