0

LINQ to Objects を使用して本のリストをグリッドビューにバインドしようとしています。Author と Book はカスタム オブジェクトで、"Books" は Author クラスでリストとして定義されます。

    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: サーバーは要求を処理できませんでした。--->

System.InvalidCastException: タイプのオブジェクトをキャストできません

「System.Collections.Generic.List`1[Book]」で「Book」と入力します。

System.Linq.Enumerable.d__aa 1.MoveNext() at System.Collections.Generic.List1..ctor (IEnumerable 1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 ソース)で

ここで何が間違っていますか?

4

2 に答える 2

0

これは単なるアイデアです。それ以外の場合は問題ないと思います。var books = (from s in author where s.LastName == "Jones" select s.Books).Tolist(); これを直接 GridView2.DataSource = books に渡します。GridView2.DataBind();

これはうまくいくかもしれません..

于 2013-05-02T04:55:32.070 に答える