0

我收到以下 linq 查询错误:

LINQ to Entities 无法识别方法 'System.Collections.Generic.List 1[System.Char] ToList[Char](System.Collections.Generic.IEnumerable1[System.Char])' 方法,并且此方法无法转换为存储表达式。

var result =
(
    from citation in Db.Citation
    where citation.IsActive == true
    select new CitationViewModel
    {
        CitationID = citation.CitationID,
        CitationTitle = citation.CitationTitle,
        DocType = citation.DocType,
        DateOfPub = citation.DateOfPub,
        authors = citation.citation_Authors.Where(c => c.CitationID == citation.CitationID).SelectMany(b => b.Author.AuthorName).ToList(),
    }
);

这是我的视图模型:

public class CitationViewModel
{
    public int CitationID { get; set; }
    public string CitationTitle { get; set; }
    public IList<char> authors { get; set; }
}

提前致谢。

4

2 回答 2

0

I'd suggest modifying your code like this (note how the authors property is initialized now) :

var result =
(
    from citation in Db.Citation
    where citation.IsActive == true
    select new CitationViewModel
    {
        CitationID = citation.CitationID,
        CitationTitle = citation.CitationTitle,
        DocType = citation.DocType,
        DateOfPub = citation.DateOfPub,
        authors = new List<char>(citation.citation_Authors.Where(c => c.CitationID == citation.CitationID).SelectMany(b => b.Author.AuthorName)),
    }
);
于 2013-06-07T17:35:39.623 回答
0

如果您将IList<char> authors属性更改CitationViewModel

public IEnumerable<string> authors { get; set; }

然后删除ToList()投影中的调用。SelectMany(b => b.Author.AuthorName)返回一个IEnumerable<string>.

于 2013-06-08T12:07:01.377 回答