It totally depends on your ORM. If utilization of ConnectionFactory.Current.GetDBConnection()
closes the connection, you can never list the result. If he does not close the connection (and something else), it may work depending on whether someone closed his connection.
In any case, you probably do not want to return an un-enumerated enumerated from what creates and deletes the connection.
either list the collection before it is closed, for example:
static IEnumerable<A> GetA() { using (var db = ConnectionFactory.Current.GetDBConnection()) { return (from a in db.A select a).ToArray(); } }
or manage the connection at a level that lists the results, for example:
static IEnumerable<A> GetA(whatevertype db) { return from a in db.A select a; } static B[] DoSmth() { using (var db = ConnectionFactory.Current.GetDBConnection()) { var aItems = GetA(db); if (!aItems.Any()) return null; return aItems.Select(a => new B(a.prop1)).ToArray(); } }
source share