【问题标题】:How to String Array convert String Value, in linq line?如何在 linq 行中将字符串数组转换为字符串值?
【发布时间】:2015-12-17 16:50:12
【问题描述】:

我使用了 string.join 但不工作。

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

Error:Message = "LINQ to Entities 无法识别方法 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' 方法,并且无法翻译此方法到商店表达式中。”

【问题讨论】:

  • 这需要认真重新考虑......在第一次选择后我迷路了很多,更不用说接下来的 3. 这对我来说是一种代码味道......
  • 没有与String.Join等价的 SQL,因此 EF 无法将其转换为 SQL。如果您可以提供有关您的数据模型的更多详细信息,您可能会在拉动这两个集合并将它们加入 Linq-to-Objects 中获得一些帮助。

标签: c# model-view-controller


【解决方案1】:

无需深入研究,解决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))))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2019-02-17
    • 2021-06-27
    • 1970-01-01
    • 2015-11-11
    • 2011-06-18
    相关资源
    最近更新 更多