编译错误:
LINQ to Entities 无法识别方法 '<> 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)' 方法,并且此方法不能转换为存储表达式。
我的方法:
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