- ROP_IDE.zip
- Clifton.Collections.Generic
- Clifton.Data
- Clifton.Services
- Clifton.Tools.Data
- Clifton.Tools.Strings
- Clifton.Windows.Forms
- DataSetDataProvider
- IDE
- Interfaces.UI
- Myxaml 2.0
- MyXaml.Core
- MyXaml.WinForms
- ROPLib.Data
- ROPLib.Graphing
- ROPLib.Interfaces
- ROPLib.RendererInterfaces
- ROPLib.Reporting
- ROPLib.UIEvents
- ROPLib.UIRendering
- ROPLib
- SQLLiteDataProvider
- UI.DotNet
- UI.DX
- UnitTest
- XTreeInterfaces
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
namespace Clifton.Tools.Data
{
public static class ExtensionMethods
{
/// <summary>
/// Return the key for the dictionary value or throws an exception if more than one value matches.
/// </summary>
public static TKey KeyFromValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TValue val)
{
// from: http://stackoverflow.com/questions/390900/cant-operator-be-applied-to-generic-types-in-c
// "Instead of calling Equals, it's better to use an IComparer<T> - and if you have no more information, EqualityComparer<T>.Default is a good choice: Aside from anything else, this avoids boxing/casting."
return dict.Single(t => EqualityComparer<TValue>.Default.Equals(t.Value, val)).Key;
}
// Note the "where" constraint, only value types can be used as Nullable<T> types.
// Otherwise, we get a bizzare error that doesn't really make it clear that T needs to be restricted as a value type.
public static object AsDBNull<T>(this Nullable<T> item) where T : struct
{
// If the item is null, return DBNull.Value, otherwise return the item.
return item as object ?? DBNull.Value;
}
/// <summary>
/// Implements a ForEach for generic enumerators.
/// </summary>
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
{
foreach (var item in collection)
{
action(item);
}
}
/// <summary>
/// Implements ForEach for non-generic enumerators.
/// </summary>
// Usage: Controls.ForEach<Control>(t=>t.DoSomething());
public static void ForEach<T>(this IEnumerable collection, Action<T> action)
{
foreach (T item in collection)
{
action(item);
}
}
public static void ForEach(this DataView dv, Action<DataRowView> action)
{
foreach (DataRowView drv in dv)
{
action(drv);
}
}
// From the comments of the blog entry http://blog.jordanterrell.com/post/LINQ-Distinct()-does-not-work-as-expected.aspx regarding why Distinct doesn't work right.
public static IEnumerable<T> RemoveDuplicates<T>(this IEnumerable<T> source)
{
return RemoveDuplicates(source, (t1, t2) => t1.Equals(t2));
}
public static IEnumerable<T> RemoveDuplicates<T>(this IEnumerable<T> source, Func<T, T, bool> equater)
{
// copy the source array
List<T> result = new List<T>();
foreach (T item in source)
{
if (result.All(t => !equater(item, t)))
{
// Doesn't exist already: Add it
result.Add(item);
}
}
return result;
}
// From http://stackoverflow.com/questions/564366/generic-list-to-datatable
// which also suggests, for better performance, HyperDescriptor: http://www.codeproject.com/Articles/18450/HyperDescriptor-Accelerated-dynamic-property-acces
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.