2015-12-17 90 views
1

我用string.join但不工作。如何字符串数组转换字符串值,在linq线?

IQueryable<CandidateVModel> list = cRepository.Table.Where(x=> !x.CFDemand.Any(y => y.AStatusId == (int) TStatus.SWork)).Select(x => new CandidateVModel 
     { 
      ... 
      Email = x.Email, 
      Tags = x.Tags, 
      Comments= string.Join(", ",x.CFDemand.SelectMany(y=>y.JInterview.SelectMany(z=>z.JInterviewer.SelectMany(t=>t.JInterviewerFeedback.Select(a=>a.Comments))))), 
      Ref= x.Ref 
     }).AsExpandable().Where(p); 

错误:消息=“LINQ实体无法识别方法 'System.String加入(System.String,System.Collections.Generic.IEnumerable`1 [System.String])' 方法,和该方法不能转换成商店表达式。“

+2

这需要认真重新思考....之后的第一选择很多更不用说接下来的3本我失去了一个代码味道对我来说...... –

+0

没有与'String.Join'等价的SQL,因此EF无法将其转换为SQL。如果您可以提供有关数据模型的更多详细信息,则可以通过拉取两个集合并将它们加入到Linq-to-Objects中来获得帮助。 –

+0

引用下面的链接: - http://stackoverflow.com/questions/4095658/how-do-i-concatenate-strings-in-entity-framework-query – Sahi

回答

0

没有挖掘太多成这样了,解决问题LINQ to Entities does not recognize the method一个办法就是简化你从数据库中获取什么,创造像这样在您的视图模型一个辅助属性:

[IgnoreDataMember] 
    public IEnumerable<string> CommentsFromDb 
    { 
     set 
     { 
      Comments = string.Join(", ", value); 
     } 
    } 

[IgnoreDataMember]将继续这个帮助属性被序列化。

和更新您的代码片段是这样的:

... 
CommentsFromDb = x.CFDemand.SelectMany(y=>y.JInterview.SelectMany(z=>z.JInterviewer.SelectMany(t=>t.JInterviewerFeedback.Select(a=>a.Comments))))