DataContext() without using statement in inline linq-to-sql queries

A

Andrus

Web services methods using Linq to sql contain lot of scalar queries like

decimal? result;
using (var db = new MyDataContext())
result = (from entity in db.SomeTable
where entity.Id=somevalue
select d.DecimalColum).SingleOrDefault();

To make code shorter those can also be re-written as

decimal? result = (from entity in new MyDataContext().SomeTable
where entity.Id=somevalue
select d.DecimalColum).SingleOrDefault();

In this case DataContext is not wrapped to using statement and maybe dispose
is not called.

Is it ok to use queries without using ?
Can this query simplified more ?


Andrus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top