我正在尝试使用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