Binding GridView to IQueryable

后端 未结 3 1356
有刺的猬
有刺的猬 2021-01-05 23:41

This question is purely academic, because I\'d never dream of doing this in real code.

Using LINQ to SQL, I want to bind an IQueryable

3条回答
  •  既然无缘
    2021-01-06 00:38

    You can think of the IQueryable as the instructions required to execute the query. When you call .ToList(), you are executing the IQueryable() to return actual data. When you bind to IQueryable(), it will expect to have a data source to get the actual data whenever DataBind() is called.

    When you set gvGenre.DataSource() = Genre.GetGenres2(), the DataContext required to get actual data based on your IQueryable is destroyed before the call to DataBind() occurs.

    It works if you call .ToList() because you're physically going out and getting the data, then putting it memory.

    Storing the IQueryable is like storing just the query. You can't execute a query if the datasource it expects to work with doesn't exist.

提交回复
热议问题