Ejemplo n.º 1
0
        /// <summary>
        ///     Returns a list of rows filtered by a filter function
        /// </summary>
        /// <param name="filterFunc"></param>
        /// <returns></returns>
        public FlexTable <T> FilteredRows(Func <T, bool> filterFunc)
        {
            var result = new FlexTable <T>(this.columnDescriptors);

            result.AddRange(this.Where(row => filterFunc(row)));

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Attaches a row set to the current row set (only new columns, does not overwrite or append)
        /// </summary>
        /// <remarks>
        ///     This will append those columns from the other row set, which are
        ///     new to the current row set. Additionally it will NOT overwrite
        ///     the values of any already existent columns of the current row set.
        ///     It will also not add any rows of the other row set, which have no
        ///     correspondent rows in the current row set.
        ///     Only the first occurence of the matching value in the other row set
        ///     will be merged (this is non-deterministic!)
        /// </remarks>
        /// <param name="other">the other row set</param>
        /// <param name="myMatchColumn">column name to match on my side</param>
        /// <param name="otherMatchColumn">column name to match on the other side</param>
        public void Attach(FlexTable <T> other, string myMatchColumn, string otherMatchColumn)
        {
            // get the list of columns, which are "new" to the current row set.
            var colList =
                other.ColumnDefinitions.Where(
                    col => !this.ColumnDefinitions.Any(myCol => myCol.ColumnPropertyName.Equals(col.ColumnPropertyName)))
                .ToList();

            if (colList.Count == 0)
            {
                return;                     // nothing to do, so we do not need to scan
            }
            // add the columns of the other list
            foreach (var column in colList)
            {
                this.AddColumn(column.ColumnTitle, column.ColumnType, column.ColumnPropertyName,
                               column.SourcePropertyName);
            }

            // now loop over the rows of the current row set
            // try to match a corresponding row in the other row set
            foreach (var row in this)
            {
                // get my matching key
                var myKey = row[myMatchColumn];
                // skip this one if the key is null or empty
                if (myKey == null || (myKey is string && string.IsNullOrEmpty((string)myKey)))
                {
                    continue;
                }

                // find the first occurence of the key on the other side
                var otherRow = other.FirstOrDefault(r => r[otherMatchColumn].Equals(myKey));

                // copy the data
                if (otherRow != null)
                {
                    foreach (var column in colList)
                    {
                        row[column.ColumnPropertyName] = otherRow[column.ColumnPropertyName];
                    }
                }
            }
        }