将GridView绑定到IQueryable< T> [英] Binding GridView to IQueryable<T>

查看:174
本文介绍了将GridView绑定到IQueryable< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题纯粹是学术问题,因为我从来没有梦想过在真正的代码中这样做。使用LINQ to SQL,我使用LINQ to SQL希望将一个 IQueryable< T> 绑定到GridView。我试着用下面的代码来做这件事,但我得到了这个异常:


无法访问已处理的对象。
对象名称:'DataContext在Dispose后访问'。


这是我的代码,它得到一个 IQueryable< tabGenre> 使用LINQ to SQL:

  public static IQueryable< lu_Genre> GetGenres2(){
using(BooksContextDataContext ctx = new BooksContextDataContext()){
IQueryable< tabGenre> iq =从ctx.tabGenre中的g开始
选择g;
返回iq;




$ b $ p
$ b

这里是我的代码,它将GridView绑定到返回 IQueryable< T>

  private void GetGenres(){
gvGenre.DataSource = Genre.GetGenres2();
gvGenre.DataBind();
}

那为什么这不起作用呢?我可以只是一个 .ToList< tabGenre>(),返回它,绑定到它然后它将工作,但为什么IQueryable不以相同的方式工作?我真正想要实现的是理解IQueryable的功能。

懒加载,但没有影响。

解决方案

您可以将IQueryable视为执行查询所需的指示信息。当您调用.ToList()时,您正在执行IQueryable()以返回实际数据。当你绑定到IQueryable()时,它会希望有一个数据源来获取每当调用DataBind()时的实际数据。



当你设置gvGenre.DataSource( )= Genre.GetGenres2(),获取基于您的IQueryable的实际数据所需的DataContext在调用DataBind()之前被销毁。



.ToList()是因为你实际上正在外出并获取数据,然后将其放入内存中。



存储IQueryable就像存储查询一样。如果它希望使用的数据源不存在,则不能执行查询。


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<T> to a GridView. I tried doing this with the following code, but I get the exception:

Cannot access a disposed object. Object name: 'DataContext accessed after Dispose.'.

Here is my code that gets an IQueryable<tabGenre> using LINQ to SQL:

public static IQueryable<lu_Genre> GetGenres2() {
    using (BooksContextDataContext ctx = new BooksContextDataContext()) {
        IQueryable<tabGenre> iq = from g in ctx.tabGenre
                                  select g;
        return iq;
    }
}

And here is my code that binds the GridView to the returned IQueryable<T>.

private void GetGenres() {
    gvGenre.DataSource = Genre.GetGenres2();
    gvGenre.DataBind();
}

So why doesn't this work? I could just a .ToList<tabGenre>(), return it, bind to it and then it would work, but why doesn't IQueryable work in the same fashion? What I'm really trying to achieve here is an understanding of what IQueryable can do.

EDIT: I also tried disabling lazy loading, but to no affect.

解决方案

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.

这篇关于将GridView绑定到IQueryable&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆