LINQ to Entities无法识别该方法

时间:2013-01-13 19:01:34

标签: c# asp.net linq

编译错误:

  

LINQ to Entities无法识别该方法   “<> F_ AnonymousType1 6[System.String,System.String,System.Collections.Generic.ICollection 1 [WcfService1.titleauthor],System.String,System.String,的System.DateTime]   的ElementAt并[d>˚F _AnonymousType1 6](System.Linq.IQueryable 1并[d> f__AnonymousType1 6[System.String,System.String,System.Collections.Generic.ICollection 1 [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

1 个答案:

答案 0 :(得分: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 };