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 / 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)")
{
@karenpayneoregon
karenpayneoregon / SeriLogCustomThemes.cs
Created February 14, 2024 10:01
SeriLog custom themes sample
using Serilog.Sinks.SystemConsole.Themes;
namespace SeriLogLibrary;
public class SeriLogCustomThemes
{
/// <summary>
/// Custom theme.<br/>
/// Text: Green<br/>
/// String: Yellow<br/>
@karenpayneoregon
karenpayneoregon / bids.sql
Created February 13, 2024 10:12
Get MAX bid for each row
CREATE TABLE #ItemBids
(
ItemID int NOT NULL IDENTITY(1,1),
ItemName varchar(20) NOT NULL,
BidPrice1 DECIMAL(19, 2) NOT NULL,
BidPrice2 DECIMAL(19, 2) NOT NULL,
BidPrice3 DECIMAL(19, 2) NOT NULL,
BidPrice4 DECIMAL(19, 2) NOT NULL,
BidPrice5 DECIMAL(19, 2) NOT NULL,
);
@karenpayneoregon
karenpayneoregon / Program.cs
Created February 12, 2024 23:12
Create query string, parse query string
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Primitives;
using System.Collections.Specialized;
using System.Diagnostics;
namespace QueryHelpersApp;
internal partial class Program
{
static void Main(string[] args)
@karenpayneoregon
karenpayneoregon / Country.cs
Created February 10, 2024 14:57
Simple example for switch and switch expression
public enum Country
{
USA,
Canada,
Mexico,
Japan
}