Getting a single Entity out of a record Set is much too complicated:

public Mandator SomeFunction( Guid mandatorID )
            TypeInfo mandatorType = Domain.Current.Model.Types[typeof(Mandator)];
            RecordSet rsIndex= mandatorType.Indexes.GetIndex("GlobalID").ToRecordSet();
            RegularTuple rangeTuple = Tuple.Create(mandatorID);
            RecordSet rsResult = rsGlobalID.Range(rangeTuple, rangeTuple);
            using (var enumerator = rsResult.ToEntities<Mandator>().GetEnumerator())
                return enumerator.Current;

It wold be nice to have a function T RecordSet.ToEntity<t>()

This thread was imported from our support forum. The original discussion may contain more detailed answer. Original topic by MSE-iT.

asked Oct 31 '08 at 13:01

Editor's gravatar image


One Answer:

Alex (Xtensive) wrote:

To be done.

Btw, we'll provide few overloads for ToEntities<...>() as well. For now it gives a bit surprising results if there are N entities of specified type in a row - i.e. it returns N times more entities (first N - for the first row, second N - for the second one, etc.). This obviously isn't good.

otto wrote:

For me this extension method works:

public static TEntity Get<TEntity, TKey>(this Session s, TKey key)
            where TEntity: Entity
            where TKey: struct
            TypeInfo type = s.Domain.Model.Types[typeof(TEntity)];
            ArgumentValidator.EnsureArgumentNotNull(type, "TEntity");
            IndexInfo primaryIndex = type.Indexes.PrimaryIndex;
            RecordSet rsResult = primaryIndex.ToRecordSet()

            MapTransform keyExtractor = null;
                var keyTupleDescriptor = s.Domain.Model.Types[typeof(TEntity)].Hierarchy.KeyTupleDescriptor;
                IEnumerable<int> columnIndexes = primaryIndex.KeyColumns
                    .Select(item => primaryIndex.Columns.IndexOf(item.Key));
                keyExtractor = new MapTransform(true, keyTupleDescriptor, columnIndexes.ToArray());

            using(var enumerator = rsResult.GetEnumerator())
                    Key k = Key.Get(typeof(TEntity), keyExtractor.Apply(TupleTransformType.TransformedTuple, enumerator.Current));
                    return k.Resolve<TEntity>();
                return null;

Of course it is not optimal, for example keyExtractor is constant for a concrete type, and the references to TypeInfo instances could be also cached.

Best regards, Otto[/code]

answered Nov 01 '08 at 15:59

Editor's gravatar image


Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA