Skip to content

Instantly share code, notes, and snippets.

@philipmat
Last active October 11, 2024 02:38
Show Gist options
  • Save philipmat/b3a0c3e81c93c6cc9f794c513cab428b to your computer and use it in GitHub Desktop.
Save philipmat/b3a0c3e81c93c6cc9f794c513cab428b to your computer and use it in GitHub Desktop.
LINQPad Extension Methods
// requires:
// Newtonsoft.Json
// System.Data.SqlClient
void Main() {
// Write code to test your extensions here. Press F5 to compile and run.
}
public static class MyExtensions {
public static void ShowSqlPrint(this System.Data.Common.DbConnection thisConnection) {
((System.Data.SqlClient.SqlConnection)thisConnection).InfoMessage += (object obj, SqlInfoMessageEventArgs e) => {
if (e.Message.Length < 2000) string.Format("\u2192 {0}", e.Message).Dump(); //\u03FE\u03FF
else e.Message.Length.Dump("Message too long");
};
}
public static IEnumerable<object> Vertical<T>(this System.Collections.Generic.IEnumerable<T> list) {
return list.Select(l => new { Item = l });
}
public static void ForEach<T>(this IEnumerable<T> @this, Action<T> action) {
foreach (T item in @this) {
action(item);
}
}
public static IEnumerable<GroupCounts<TKey>> GroupByCounts<TSource, TKey>(
this IQueryable<TSource> query,
Expression<Func<TSource, TKey>> keySelector)
=> query.GroupBy(keySelector)
.Select(g => new GroupCounts<TKey>(g.Key, g.Count()));
// public static string BinaryToEncodedString(this System.Data.Linq.Binary bin) => Encoding.Default.GetString(bin.ToArray());
public static void CopyToClipboard(this string stringToCopy) => System.Windows.Clipboard.SetText(stringToCopy);
public static string Base64Decode(this string base64String) => Encoding.Default.GetString(Convert.FromBase64String(base64String));
public static string Base64Endcode(this string value) => Convert.ToBase64String(Encoding.Default.GetBytes(value));
/// <summary> using (var t = BeginTransaction) { ... t.Complete(); } </summary>
public static TransactionScope BeginTransaction() => new TransactionScope();
public static object DeserializeJson(this string jsonString)
=> Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);
public static string PrettyJson(this string jsonString)
=> Newtonsoft.Json.JsonConvert.SerializeObject(Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString), Newtonsoft.Json.Formatting.Indented);
public static string ToJsonString(this object source, bool forceCamelCase = false) {
if (source == null) {
return string.Empty;
}
if (forceCamelCase) {
var camelCaseSerializer = new Newtonsoft.Json.JsonSerializerSettings {
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
Formatting = Newtonsoft.Json.Formatting.Indented,
};
return Newtonsoft.Json.JsonConvert.SerializeObject(source, camelCaseSerializer);
}
return Newtonsoft.Json.JsonConvert.SerializeObject(source);
}
// https://stackoverflow.com/a/26668233
public static IQueryable<T> DumpNoLock<T>(this IQueryable<T> query) {
using (var txn = GetNewReadUncommittedScope()) {
return query.Dump();
}
}
public static System.Transactions.TransactionScope GetNewReadUncommittedScope() {
return new System.Transactions.TransactionScope(
System.Transactions.TransactionScopeOption.RequiresNew,
new System.Transactions.TransactionOptions {
IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
});
}
public static IQueryable<T> DumpNoLock<T>(this IQueryable<T> query, string description) {
using (var txn = GetNewReadUncommittedScope()) {
return query.Dump(description);
}
}
public static List<T> ToListNoLock<T>(this IQueryable<T> query) {
using (var txn = GetNewReadUncommittedScope()) {
return query.ToList();
}
}
/// <example>someQuery.NoLock(x => x.Count()).Dump();</example>
public static U NoLock<T, U>(this IQueryable<T> query, Func<IQueryable<T>, U> expr) {
using (var txn = GetNewReadUncommittedScope()) {
return expr(query);
}
}
}
// You can also define non-static classes, enums, etc.
public class GroupCounts<TKey> {
internal GroupCounts(TKey key, int counts) {
Key = key;
KeyCount = counts;
}
public TKey Key { get; }
public int KeyCount { get; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment