This project is read-only.

Execute Scalar or fill anonymous types

Oct 21, 2009 at 9:23 AM
Edited Oct 21, 2009 at 9:54 AM

Hi, just wondering if anyone knows how to execute a scalar command (i.e. I want a record count or to lookup a value without loading an entire object or collection of objects).

I found if you convert the ObjectContext.Storage to the type of storage you are working with you can call CreateConnection & CreateCommand, is there any reason IStorage doesn't implement these returning IDbCommand or DbCommand (etc)?

Whats the best way to ObjectContext.ExecuteScalar(Of T)(IQuery) ?

Also, is it possible to fill an anonomus type? (i.e. creating custom dynamic objects for reporting or lookups)

Opf3 Rocks!!!

Oct 22, 2009 at 5:37 PM

As far as I know, I don't think it can be done. I've resorted to making a simple class to accept a type and return the value.

	[Persistent( "BASETYPE", PoolSize = 10, Rights = PersistentRights.Load )]
	public class BaseType<T> : ISelfContainingObject
		private T _Value;

		[Field( "VALUE" )]
		public T Value
			get { return _Value; }
			set { _Value = value; }

		public ObjectInfo _ObjectInfo = new ObjectInfo();
		ObjectInfo ISelfContainingObject.ObjectInfo
			get { return _ObjectInfo; }
			set { _ObjectInfo = value; }

Oct 23, 2009 at 6:32 AM

The pattern that mtycholaz uses is the one that I use also most often. We could think about exposing the Scalar method. Probably something that might come in the future.

Oct 23, 2009 at 6:35 AM

Another way, which is kind of like the low level approach, is to use the SqlRawStorageAccess (in the Storages namespace) class. This one allows you to create a command that will run on the current open connection, and if a transaction has been started, in the current transaction.

Oct 26, 2009 at 4:57 AM

Awesome, thanks for the response guys, converted the example to vb, plugged in a few extension methods & boom, context.ExecuteScalar(Of T)(IQuery) =  :D YAY!

I would like to see IStorage have a .CreateConnection (as most/all storage providers seem to implement this), gave it a go but my C# isn't up to scratch, lol.

On a similar note, for reporting & aggregate queries do you do something similar? (i.e. define a proxy class)

Thanks again :D