Browse Source

Improved performance of ExtractValues on CoreTable

Kenric Nugteren 9 months ago
parent
commit
c5bd62d815
2 changed files with 23 additions and 10 deletions
  1. 21 8
      InABox.Core/CoreTable/CoreTable.cs
  2. 2 2
      InABox.Core/ICoreTable.cs

+ 21 - 8
InABox.Core/CoreTable/CoreTable.cs

@@ -441,20 +441,30 @@ namespace InABox.Core
 
         #region Extract Values
         
-        public IEnumerable<TValue> ExtractValues<TSource, TValue>(Expression<Func<TSource, TValue>> column, bool distinct = true)
+        public TValue[] ExtractValues<TSource, TValue>(Expression<Func<TSource, TValue>> column, bool distinct = true)
         {
-            var result = Rows.Select(r => r.Get(column));
+            var columnIdx = GetColumnIndex(column);
             if (distinct)
-                result = result.Distinct();
-            return result;
+            {
+                return Rows.Select(r => r.Get<TValue>(columnIdx)).Distinct().ToArray();
+            }
+            else
+            {
+                return Rows.ToArray(r => r.Get<TValue>(columnIdx));
+            }
         }
 
-        public IEnumerable<TValue> ExtractValues<TValue>(string column, bool distinct = true)
+        public TValue[] ExtractValues<TValue>(string column, bool distinct = true)
         {
-            var result = Rows.Select(r => r.Get<TValue>(column));
+            var columnIdx = GetColumnIndex(column);
             if (distinct)
-                result = result.Distinct();
-            return result;
+            {
+                return Rows.Select(r => r.Get<TValue>(columnIdx)).Distinct().ToArray();
+            }
+            else
+            {
+                return Rows.ToArray(r => r.Get<TValue>(columnIdx));
+            }
         }
 
         #endregion
@@ -475,6 +485,9 @@ namespace InABox.Core
         public int GetColumnIndex<T>(Expression<Func<T, object>> column)
             => GetColumnIndex(CoreUtils.GetFullPropertyName(column, "."));
 
+        public int GetColumnIndex<T, TValue>(Expression<Func<T, TValue>> column)
+            => GetColumnIndex(CoreUtils.GetFullPropertyName(column, "."));
+
         public int GetColumnIndex(string columnname)
         {
             for (var i = 0; i < Columns.Count; i++)

+ 2 - 2
InABox.Core/ICoreTable.cs

@@ -15,8 +15,8 @@ namespace InABox.Core
 
         void CopyTo(CoreTable table);
         void CopyTo(DataTable table);
-        IEnumerable<TValue> ExtractValues<TSource, TValue>(Expression<Func<TSource, TValue>> column, bool distinct = true);
-        IEnumerable<TValue> ExtractValues<TValue>(string column, bool distinct = true);
+        TValue[] ExtractValues<TSource, TValue>(Expression<Func<TSource, TValue>> column, bool distinct = true);
+        TValue[] ExtractValues<TValue>(string column, bool distinct = true);
         void Filter(Func<CoreRow, bool> predicate);
         void LoadColumns(Type T);
         void LoadDictionary<T, TKey, TValue>(Dictionary<TKey, TValue> dictionary, Expression<Func<T, TKey>> key, Expression<Func<T, TValue>> value);