/// <summary> /// Selects rows in which the value of the given column matches the given criteria. /// </summary> /// <typeparam name="T">The type of the row data.</typeparam> /// <param name="columnName">The name of the column.</param> /// <param name="selector">A function which accepts or rejects values.</param> /// <returns>A new view, containing only those rows in which the <paramref name="columnName"/> value was accepted by /// the <paramref name="selector"/>.</returns> public DataView Where <T> (string columnName, Func <T, bool> selector) { if (columnName == null) { throw new ArgumentNullException(nameof(columnName)); } if (selector == null) { throw new ArgumentNullException(nameof(selector)); } int columnIndex = GetColumnIndex(columnName); IReadOnlyDataList <T> column = (IReadOnlyDataList <T>)columns[columnIndex]; List <int> newMap = new List <int>(); for (int i = 0; i < map.Count; i++) { T value = column[map[i]]; bool include = selector(value); if (include) { newMap.Add(map[i]); } } return(new DataView(this.columns, newMap)); }
/// <summary> /// Sorts all rows by the given function of the given column. /// </summary> /// <typeparam name="T">The type of values being compared.</typeparam> /// <param name="columnName">The column to sort on.</param> /// <param name="comparer">A comparison function of <paramref name="columnName"/> values.</param> /// <returns>A view of the data sorted by the function of the given column.</returns> public DataView OrderBy <T>(string columnName, Comparison <T> comparer) { if (columnName == null) { throw new ArgumentNullException(nameof(columnName)); } if (comparer == null) { throw new ArgumentNullException(nameof(comparer)); } int columnIndex = GetColumnIndex(columnName); IReadOnlyDataList <T> column = (IReadOnlyDataList <T>)columns[columnIndex]; List <int> newMap = new List <int>(map); newMap.Sort((i, j) => comparer(column[i], column[j])); return(new DataView(this.columns, newMap)); }