我正在嘗試使用LINQ to Objects將書籍列表綁定到gridview。作者和書籍是自定義對象,其中「作品」在作者類中定義爲列表。LINQ to Objects轉換爲列表以顯示在gridview中
List<Author> authors = new List<Author>();
// list of authors/books is populated here
var books = from s in authors
where s.LastName == "Jones"
select s.Books;
GridView2.DataSource = books.AsEnumerable().Cast<Book>().ToList();
GridView2.DataBind();
不過,我得到以下錯誤:
System.Web.Services.Protocols.SoapException: Server was unable to process request. --->
System.InvalidCastException: Unable to cast object of type
'System.Collections.Generic.List`1[Book]' to type 'Book'.
at System.Linq.Enumerable.d__aa
1.MoveNext() at System.Collections.Generic.List
1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source)
我在做什麼錯在這裏?
可能您有2種不同的書籍類型。一個來自webservice,另一個來自你的項目。 – Andrei 2013-05-02 09:40:13