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 / EntityHelpers.cs
Created November 3, 2024 13:46
EF Core does database exists method
using yournamespace.Data;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
namespace yournamespace.Classes;
internal class EntityHelpers
{
public static bool DatabaseExists()
{
using var context = new Context();
@karenpayneoregon
karenpayneoregon / Demo.cs
Created November 2, 2024 00:43
int to string JsonConverter
List<Item> items =
[
new() { Id = 1,Name = "100"},
new() { Id = 2,Name = "100"},
new() { Id = 3,Name = "100" }
];
string json = JsonSerializer.Serialize(items, Options);
@karenpayneoregon
karenpayneoregon / Extensions.cs
Created October 30, 2024 15:22
Simple paging
internal static partial class Extensions
{
/// <summary>
/// Paginates the elements of an <see cref="IQueryable{TSource}"/> based on the specified page number and page size.
/// </summary>
/// <typeparam name="TSource">The type of the elements of the source.</typeparam>
/// <param name="source">The source <see cref="IQueryable{TSource}"/> to paginate.</param>
/// <param name="page">The page number to retrieve. Must be greater than or equal to 1.</param>
/// <param name="pageSize">The number of elements per page. Must be greater than or equal to 1.</param>
@karenpayneoregon
karenpayneoregon / DiacriticsConverter.cs
Last active October 28, 2024 14:05
Experimenting with multiple versions of code
public class DiacriticsConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.GetString() ?? string.Empty;
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ReplaceDiacritics2());
}
}
@karenpayneoregon
karenpayneoregon / JsonSerialization.js
Created October 24, 2024 15:00
David McCarter JsonSerialization class
#nullable enable
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
namespace Tinkering.Classes;
/// <summary>
/// Provides methods for serializing and deserializing objects to and from JSON format.
/// Base code from David McCarter
/// https://github.com/RealDotNetDave/dotNetTips.Spargine/blob/main/source/6/dotNetTips.Spargine.6.Core/Serialization/JsonSerialization.cs
/// </summary>
@karenpayneoregon
karenpayneoregon / Example.cs
Last active October 24, 2024 12:15
UpperCaseFirstCharConverter JsonConverter
string json =
/*lang=json*/
"""
[
{
"Id": 1,
"FirstName": "jose",
"LastName": "fernandez",
"BirthDate": "1985-01-01"
},
@karenpayneoregon
karenpayneoregon / Customer1.cs
Last active October 18, 2024 10:18
Examples for working with different ways to create json and deserialize
public class Customer1
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
@karenpayneoregon
karenpayneoregon / Category.cs
Created October 16, 2024 16:00
Clone record in C#
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
}
@karenpayneoregon
karenpayneoregon / Extensions.cs
Created October 14, 2024 12:42
Extract quoted text in a string
using System.Text.RegularExpressions;
namespace Your_namespace;
internal static partial class Extensions
{
public static List<string> StringsBetweenQuotes(this string sender)
{
var matches = QuotesRegex().Matches(sender);
@karenpayneoregon
karenpayneoregon / DataGridViewExtensions.cs
Last active October 13, 2024 10:53
Export unbound DataGridView to CSV file
internal record RowRecord(DataGridViewRow Row, string RowItem);
public static class DataGridViewExtensions
{
public static void ExportRows(this DataGridView sender, string fileName, string defaultNullValue = "(empty)")
{
File.WriteAllLines(fileName, sender.Rows.Cast<DataGridViewRow>()
.Where(row => !row.IsNewRow)
.Select(row => new RowRecord(