Binding GridView to IQueryable

后端 未结 3 1340
有刺的猬
有刺的猬 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:31

    The sole effect of that using block is to limit the lifespan of ctx to the block itself.

    The useful lifespan of iq is dependent on the lifespan of ctx. You are explicitly disposing of ctx before iq is ever used.

    The way to fix the problem would be to get rid of the using block, or to force iq to evaluate within the block (e.g. iq = iq.ToList()), and return only the results of that evaluation. If you're not keen on doing the latter, just ditch the using. ctx will be disposed of at some point after nobody needs it. Not perfect, maybe, but that's life with a garbage collector. Are you sure it'll be creating a problem if it stays around? Don't fix what's not yet shown to be broken.

提交回复
热议问题