All C# Extension Methods
-
OrderBy(string sortExpression)
Orders a list based on a sortexpression. Useful in object databinding scenarios where the objectdatasource generates a dynamic sortexpression (example: "Name desc") that specifies the property of the object sort on.
-
WhereIf
When building a LINQ query, you may need to involve optional filtering criteria. Avoids if statements when building predicates & lambdas for a query. Useful when you don't know at compile time whether a filter should apply.
Borrowed from Andrew Robinson.
http://bit.ly/1V36G9
-
String Extensions
String extensions, download CoreSystem Library from codeplex; http://coresystem.codeplex.com
-
DataReader to CSV
Export DataReader to CSV (List
). Basic example that to export data to csv from a datareader. Handle value if it contains the separator and/or double quotes but can be easily be expended to include culture (date, etc...) , max errors, and more. -
ToDictionary() - for enumerations of groupings
Converts an IEnumerable
> to a Dictionary > so that you can easily convert the results of a GroupBy clause to a Dictionary of Groupings. The out-of-the-box ToDictionary() LINQ extension methods require a key and element extractor which are largely redundant when being applied to an enumeration of groupings, so this is a short-cut.
-
Pivot
Groups the elements of a sequence according to a specified firstKey selector function and rotates the unique values from the secondKey selector function into multiple values in the output, and performs aggregations.
-
FormatWithMask
Formats a string with the specified mask
-
ForEach
Shortcut for foreach and create new list
-
AddRange<T>()
I have created this AddRange
() method on ObservableCollection because the LINQ Concat() method didn't trigger the CollectionChanged event. This method does. -
ToPagedList
Paging extension method for NHibernate 3.0 and its new query API QueryOver.
-
Encrypt & Decrypt
Encrypt and decrypt a string using the RSACryptoServiceProvider.
-
GetCurrentDataRow
The System.Windows.Forms.BindingSource has a property to return the current row as DataRowView object. But most of the time we need this as DataRow to manipulate the data. This extension method checks the Current property of BindingSource for nullity, returns null if it is null and returns the current Row as DataRow object if the Current property is not null.
-
Enum<T>.Parse() and Enum<T>.TryParse()
Parses the specified string value into the Enum type passed. Also contains a bool to determine whether or not the case should be ignored.
-
ToObservableCollection<T>()
Convert a IEnumerable
to a ObservableCollection and can be used in XAML (Silverlight and WPF) projects -
ToDataTable
Converts an IEnumerable to DataTable (supports nullable types)
adapted from http://www.c-sharpcorner.com/UploadFile/VIMAL.LAKHERA/LINQResultsetToDatatable06242008042629AM/LINQResultsetToDatatable.aspx
-
ToJson() and FromJson<T>()
Convert an object to JSON an back
-
Generic Enum to List<T> converter
http://devlicio.us/blogs/joe_niland/archive/2006/10/10/Generic-Enum-to-List_3C00_T_3E00_-converter.aspx
-
IList<T> to Excel file
An extension method that produce a excel file of List
. This would be useful if you want to automatically generate a Excel out of any other primative data type collection I provided 1 overloads of this method, that accepts a Path as string to save excel file to location on disk. -
UcFirst
Emulation of PHPs ucfirst()
-
IsNumeric
Checks if a string value is numeric according to you system culture.
-
DataTable to CSV export
Export datatable to CSV file
-
Clone<T>()
Makes a copy from the object.
-
SafeInvoke
Properly invokes an action if it is required. Best way to handle events and threaded operations on a form.
-
ComputeHash
Computes the hash of a string using one of the following algorithms: HMAC, HMACMD5, HMACSHA1, HMACSHA256, HMACSHA384, HMACSHA512,MACTripleDES, MD5, RIPEMD160, SHA1, SHA256, SHA384, SHA512.
-
ToSecureString
Converts a string into a "SecureString"
-
Strip Html
Removes any HTML or XML tags from a string. Super simple, but I didn't see anything here like it. I've created similar methods in the past to take into account things like