0

编译错误:

LINQ to Entities 无法识别方法 '<> f_AnonymousType1 6[System.String,System.String,System.Collections.Generic.ICollection1[WcfService1.titleauthor],System.String,System.String,System.DateTime] ElementAt[<>f _AnonymousType1 6](System.Linq.IQueryable1[<>f__AnonymousType1 6[System.String,System.String,System.Collections.Generic.ICollection1[WcfService1.titleauthor], System.String,System.String,System.DateTime]], Int32)' 方法,并且此方法不能转换为存储表达式。

我的方法:

public PublicationDetail GetPublicationDetails(string PubID)
{
    PublicationDetail pub = null;
    PubsEntities db = new PubsEntities();

    // Get publication details
    var lookup = from entries in db.titles
                 where entries.title_id.Equals(PubID)
                 select new
                 {
                     PubID = entries.title_id,
                     Title = entries.title1,
                     AuthorsListId = entries.titleauthors,
                     Description = entries.notes,
                     Publisher = entries.pub_id,
                     PubDate = entries.pubdate
                 };

    // Get authors list
    var alookup = from authors in db.titleauthors
                  where authors.title_id.Equals(PubID)
                  select new { AuthorID = authors.au_id };

    // Get authors
    List<string> pub_atuhors = new List<string>();
    foreach (var auth in alookup)
    {
        // Get id
        var id = auth.AuthorID;

        var getAuthor = from authors in db.authors
                        where authors.au_id.Equals(id)
                        select new { Author = authors.au_fname + " " + authors.au_lname };

        pub_atuhors.Add(getAuthor.ElementAt(0).Author);
    }

    // Get publisher
    var lookupPublisher = from publishers in db.publishers
                          where publishers.pub_id.Equals(lookup.ElementAt(0).Publisher)
                          select new { PublisherName = publishers.pub_name };

    pub = new PublicationDetail
    {
        PubID = lookup.ElementAt(0).PubID,
        Title = lookup.ElementAt(0).Title,
        Description = lookup.ElementAt(0).Description,
        PubDate = lookup.ElementAt(0).PubDate,
        Publisher = lookupPublisher.ElementAt(0).PublisherName,
        Authors = pub_atuhors
    };

    return pub;
}

错误显示在返回语句之前的最后一行 VS2012 突出显示该方法的以下部分:

pub = new PublicationDetail
{
    PubID = lookup.ElementAt(0).PubID,
    Title = lookup.ElementAt(0).Title,
    Description = lookup.ElementAt(0).Description,
    PubDate = lookup.ElementAt(0).PubDate,
    Publisher = lookupPublisher.ElementAt(0).PublisherName,
    Authors = pub_atuhors
}; // <- Error is shown here
4

1 回答 1

1

只需将您的变量存储lookup.ElementAt(0).Publisher在某个变量中并对其进行操作即可。

喜欢(不检查null):

var publisher_id = lookup.First().Publisher;

var lookupPublisher = from publishers in db.publishers
                      where publishers.pub_id == publisher_id
                      select new { PublisherName = publishers.pub_name };
于 2013-01-13T19:22:03.237 回答