編譯錯誤: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_AnonymousType16](System.Linq.IQueryable
1[<>f__AnonymousType16[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
現在,它在新PublicationDetail結束,但在VAR PUBLISHER_ID = lookup.First()發佈結束不再給了一個錯誤;序列不包含任何元素 –
使用Console.Write我回溯到第一個數據庫查找有一些錯誤。 –
好像你的'lookup'集合是空的。也不要忘記實現你的Linq查詢。 –