Skip to content

Instantly share code, notes, and snippets.

View karenpayneoregon's full-sized avatar
🎯
Focusing

Karen Payne karenpayneoregon

🎯
Focusing
View GitHub Profile
@karenpayneoregon
karenpayneoregon / Example.cs
Created April 4, 2024 13:45
Get DotNet version at runtime
public static Version FrameworkRuntimeVersion()
{
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var path = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
var index = Array.IndexOf(path, "Microsoft.NETCore.App");
return new Version((index > 0 && index < path.Length - 2 ? path[index + 1] : null)!);
}
static void Main(string[] args)
{
int current = 20240403;
int birthDate = 19000924;
Console.WriteLine((current - birthDate).ToString()[..^4]);
Console.WriteLine((current - birthDate).ToString().RemoveLastCharacters());
@karenpayneoregon
karenpayneoregon / script.sql
Created March 30, 2024 01:08
Get all databases and table names for SQL-Server
DECLARE @sql NVARCHAR(MAX);
SELECT @sql
= ( SELECT ' UNION ALL
SELECT ' + +QUOTENAME(name, '''')
+ ' as DatabaseName,
s.name COLLATE DATABASE_DEFAULT
AS SchemaName,
t.name COLLATE DATABASE_DEFAULT as TableName
FROM ' + QUOTENAME(name) + '.sys.tables t
public interface ISample
{
public void AcceptCallMethod();
void CallerMethod() { }
}
public class Sample : ISample
{
void ISample.AcceptCallMethod()
{
@karenpayneoregon
karenpayneoregon / Extensions.cs
Last active April 4, 2024 00:44
Working with strings and ternary operator
using System.Text.RegularExpressions;
public static class StringExtensions
{
// remove double spaces
public static string RemoveExtraSpaces(this string sender, bool trimEnd = false)
{
const RegexOptions options = RegexOptions.None;
var regex = new Regex("[ ]{2,}", options);
var result = regex.Replace(sender, " ");
@karenpayneoregon
karenpayneoregon / Utilities.cs
Last active March 15, 2024 22:22
Get .NET Core runtime version
public static string GetNetCoreVersion()
{
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];
return null;
}
@karenpayneoregon
karenpayneoregon / readme.md
Last active March 9, 2024 16:03
Get table names and row count for a SQL-Server database

First statement concatenates schema with table name while the second statement separates schema name and table name.

public void UpdateSpecificField(Employees entity, params Expression<Func<Employees, object>>[] updatedProperties)
{
foreach (var property in updatedProperties)
{
_context.Entry(entity).Property(property).IsModified = true;
}
}
public static class Extensions
{
public static void ForEachWithIndex<T>(this IEnumerable<T> sender, Action<T, int> handler)
{
int idx = 0;
foreach (T item in sender)
{
handler(item, idx++);
}
}
@karenpayneoregon
karenpayneoregon / DataGridViewExtensions.cs
Last active February 17, 2024 01:58
Export unbound DataGridView to a comma delimited file
public static class DataGridViewExtensions
{
/// <summary>
/// Write entire contents, rows and cells to a text file
/// </summary>
/// <param name="sender">DataGridView with rows and columns</param>
/// <param name="fileName">Path and file name to write too</param>
/// <param name="defaultNullValue">Default value for null or empty cells</param>
public static void ExportRows(this DataGridView sender, string fileName, string defaultNullValue = "(empty)")
{