开发者

What is the benefit of disposing of a LINQ to SQL DataContext?

What is the benefit of disposing of a LINQ to SQL DataContext? Or, is there a开发者_C百科 problem with not disposing of these DataContext objects?

For instance, for easy coding, I might want to do something like...

var list = from p in (new MyDataContext()).People where p.LastName.Contains("sommar") select p;

In this case, I have new'd up an instance but not "closed" it or disposed of it in any way. Is it still out there floating around with potential to cause me problems? To up the ante, let's put that in a loop where it gets called 2000 times. <italian:accent>You got a problem with that?</italian:accent>


L2S data contexts don't need to be closed or disposed. However, I don't think I would want to do what you are doing, newing up a new one 2000 times in a loop. A data context is a heavy weight object, much like a SQLConnection on major steroids. I wouldn't want the overhead of creating 2000 in a loop. OTOH, you don't want them living for too long either.

New one up, do your unit of work and then let the framework dispose of it, or do it yourself. There is no harm in doing so.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜