/// <summary> /// Converts the <see cref="RecordSetReader"/> items to <see cref="Entity"/> instances. /// </summary> /// <param name="source">The <see cref="RecordSetReader"/> to process.</param> /// <param name="primaryKeyIndex">Index of primary key within the <see cref="Record"/>.</param> /// <returns>The sequence of <see cref="Entity"/> instances.</returns> public static IEnumerable <Entity> ToEntities(this RecordSetReader source, int primaryKeyIndex) { var session = ((EnumerationContext)source.Context).Session; var reader = session.Domain.EntityDataReader; foreach (var record in reader.Read(source.ToEnumerable(), source.Header, session)) { var key = record.GetKey(primaryKeyIndex); if (key == null) { continue; } var tuple = record.GetTuple(primaryKeyIndex); if (tuple != null) { yield return(session.Handler.UpdateState(key, tuple).Entity); } else { yield return(session.Query.SingleOrDefault(key)); } } }