Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit a57db9ad authored by Michael KRISPER's avatar Michael KRISPER
Browse files

useful Extensionmethods for Dictionaries and Datatables.

parent a7c03d01
No related branches found
No related tags found
No related merge requests found
......@@ -794,10 +794,7 @@ namespace TUGraz.VectoCore.InputData.FileIO.JSON
}
for (var i = 0; i < component.Count; i++) {
if (!_componentDigests.ContainsKey(component.Entry)) {
_componentDigests[component.Entry] = new List<string>();
}
_componentDigests[component.Entry].Add(
_componentDigests.GetOrAdd(component.Entry, _=> new List<string>()).Add(
XMLManufacturerReportReader.GetComponentDataDigestValue(xmlDoc, component.Entry, i));
}
}
......
......@@ -29,11 +29,16 @@
* Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology
*/
using Ninject.Planning.Bindings.Resolvers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.ServiceModel.Security;
using System.Windows.Forms.DataVisualization.Charting;
using TUGraz.VectoCommon.Exceptions;
using TUGraz.VectoCommon.InputData;
using TUGraz.VectoCommon.Utils;
namespace TUGraz.VectoCore.Utils
......@@ -51,6 +56,22 @@ namespace TUGraz.VectoCore.Utils
return defaultValue;
}
public static IEnumerable<TResult> SelectRows<TResult>(this DataTable self, Func<DataRow,TResult> selector) => self.Rows.Cast<DataRow>().Select(selector);
public static IEnumerable<TResult> SelectRows<TResult>(this DataTable self, Func<DataRow, int, TResult> selector) => self.Rows.Cast<DataRow>().Select(selector);
public static IEnumerable<DataRow> Where(this DataTable self, Func<DataRow, bool> predicate) => self.Rows.Cast<DataRow>().Where(predicate);
public static IEnumerable<DataRow> Where(this DataTable self, Func<DataRow, int, bool> predicate) => self.Rows.Cast<DataRow>().Where(predicate);
public static DataRow First(this DataTable self) => self.Rows.Cast<DataRow>().First();
public static DataRow First(this DataTable self, Func<DataRow, bool> predicate) => self.Rows.Cast<DataRow>().First(predicate);
public static DataRow Last(this DataTable self) => self.Rows.Cast<DataRow>().Last();
public static double Sum(this DataTable self, Func<DataRow, double> selector) => self.Rows.Cast<DataRow>().Sum(selector);
public static double ParseDouble(this DataRow row, int columnIndex)
{
return row.ParseDouble(row.Table.Columns[columnIndex]);
......
......@@ -29,23 +29,52 @@
* Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology
*/
using Ninject.Planning.Bindings.Resolvers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows.Forms;
using TUGraz.VectoCommon.Utils;
namespace TUGraz.VectoCore.Utils
{
internal static class DictionaryExtensionMethods
{
public static object GetValueOrNull<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
return dictionary.TryGetValue(key, out var value) ? (object)value : DBNull.Value;
}
public static object GetValueOrNull<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) =>
dictionary.TryGetValue(key, out var value) ? (object)value : DBNull.Value;
public static TValue GetValueOrZero<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
where TValue : SIBase<TValue>
where TValue : SIBase<TValue> => dictionary.TryGetValue(key, out var value) ? value : 0.SI<TValue>();
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key, Func<TKey, TValue> defaultConstructor)
{
return dictionary.TryGetValue(key, out var value) ? value : 0.SI<TValue>();
if (self.TryGetValue(key, out var result))
return result;
lock (self) {
if (!self.TryGetValue(key, out result)) {
result = defaultConstructor(key);
self.Add(key, result);
}
return result;
}
}
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key) =>
self.TryGetValue(key, out var value) ? value : default;
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key, TValue defaultValue) =>
self.TryGetValue(key, out var value) ? value : defaultValue;
/// <summary>
/// Joins the items of the enumerable into a string.
/// </summary>
/// <param name="self"></param>
/// <param name="separator"></param>
/// <returns></returns>
public static string JoinString(this IEnumerable<string> self, string separator = ", ") => string.Join(separator, self ?? Enumerable.Empty<string>());
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment