/// <summary> /// Makes a best effort to parse the row into a strongly typed object. /// It's much faster to call DataTable.RowsAs[T] instead, because that will reuse parsing logic across rows. /// This does a case-insensitive match of the Target object's property names against the table's column names. /// Parse errors are ignored and may produce invalid results for the corresponding cell. /// </summary> /// <typeparam name="T">Target object type to parse.</typeparam> /// <param name="row">incoming row to be parsed</param> /// <returns>an object representing the row</returns> public static T As <T>(this Row row) where T : new() { var parser = StrongTypeBinder.BuildMethod <T>(row.ColumnNames); var c = parser(row); return(c); }
/// <summary> /// Enumeration of rows as strongly types. The default implementation here is /// to just to parse the results of <see cref="Rows"/>. /// </summary> /// <typeparam name="T">Target object type to parse.</typeparam> /// <returns>enumeration of rows as strongly typed object</returns> public virtual IEnumerable <T> RowsAs <T>() where T : class, new() { Func <Row, T> parser; // Get cached version of the function. // This is optimized assuming that all reads are for the same schema. // This code should be thread safe. { parser = _parserFunc as Func <Row, T>; if (parser == null) { parser = StrongTypeBinder.BuildMethod <T>(this.ColumnNames); _parserFunc = parser; } } // Use the local parser function, not the field, in case the field is switched on us by another thread. var result = from row in Rows select parser(row); return(result); }
/// <summary> /// Return a delegate that parses a Row from this DataTable into a given T. /// This is useful if you need to cache the parser function /// </summary> /// <returns>the strongly typed object.</returns> public Func <Row, T> GetParserFunction <T>() { return(StrongTypeBinder.BuildMethod <T>(this.ColumnNames)); }
/// <summary> /// Return a delegate that parses a Row from this DataTable into a given T. /// This is useful if you need to cache the parser function /// </summary> /// <returns>the strongly typed object.</returns> public Func <Row, T> GetParserFunction <T>(Dictionary <string, string> mappingDictionary = null) { return(StrongTypeBinder.BuildMethod <T>(this.ColumnNames, mappingDictionary)); }
/// <summary> /// Return a fast compiled function that parses rows into a strongly typed object. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="columnNames">column names that will correspond to the row.</param> /// <returns>the parsing function</returns> public static Func <Row, T> BuildMethod <T>(IEnumerable <string> columnNames) { return(StrongTypeBinder.BuildMethod <T>(columnNames)); }