2013-01-13 84 views
0

编译错误:LINQ到实体无法识别方法

LINQ to Entities does not recognize the method '<>f_AnonymousType1 6[System.String,System.String,System.Collections.Generic.ICollection 1[WcfService1.titleauthor],System.String,System.String,System.DateTime] ElementAt[<>f_AnonymousType1 6](System.Linq.IQueryable 1[<>f__AnonymousType1 6[System.String,System.String,System.Collections.Generic.ICollection 1[WcfService1.titleauthor],System.String,System.String,System.DateTime]], Int32)' method, and this method cannot be translated into a store expression.

我的方法:

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

误差return语句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

只需将您的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 }; 
+0

现在,它在新PublicationDetail结束,但在VAR PUBLISHER_ID = lookup.First()发布结束不再给了一个错误;序列不包含任何元素 –

+0

使用Console.Write我回溯到第一个数据库查找有一些错误。 –

+0

好像你的'lookup'集合是空的。也不要忘记实现你的Linq查询。 –

相关问题