/// <summary> /// Converts a sequence of <paramref name="items"/> into a sequence of <see cref="SqlDataRecord"/> using the supplied <paramref name="metaData"/> /// </summary> internal static IEnumerable <SqlDataRecord> ToDataRecords <T>(this IEnumerable <T> items, SqlMetaData[] metaData) { Contract.Requires(items != null); Contract.Requires(metaData != null); Contract.Requires(metaData.Length > 0); Contract.Ensures(Contract.Result <IEnumerable <SqlDataRecord> >() != null); var key = new TypeAndMetaData(typeof(T), metaData); var map = (Func <SqlMetaData[], T, SqlDataRecord>)GetOrAddFunc(key, typeof(T)); return(items.Select(item => map(metaData, item))); }
/// <summary> /// Converts a sequence of <paramref name="items"/> into a <see cref="TableType"/> containing a sequence of <see cref="SqlDataRecord"/> using the supplied <paramref name="metaData"/> /// </summary> /// <remarks><paramref name="extraAction"/> can be used to set additional values on each <see cref="SqlDataRecord"/></remarks> public static TableType ToTableType <T>(this IEnumerable <T> items, SqlMetaData[] metaData, string tableTypeName, Action <SqlDataRecord, T> extraAction) { Contract.Requires(items != null); Contract.Requires(metaData != null); Contract.Requires(metaData.Length > 0); Contract.Requires(!string.IsNullOrWhiteSpace(tableTypeName)); Contract.Ensures(Contract.Result <IEnumerable <SqlDataRecord> >() != null); var key = new TypeAndMetaData(typeof(T), metaData); var map = (Func <SqlMetaData[], T, SqlDataRecord>)GetOrAddFunc(key, typeof(T)); return(new TableType(tableTypeName, ConvertItemsToRecords(items, metaData, map, extraAction))); }
static Delegate GetOrAddFunc(TypeAndMetaData key, Type typeT) => Methods.GetOrAdd(key, data => CreateMappingFunc(typeT, data.MetaData));