Skip to content

Instantly share code, notes, and snippets.

View csharpforevermore's full-sized avatar
🏠
Working from home

Randle csharpforevermore

🏠
Working from home
View GitHub Profile
@csharpforevermore
csharpforevermore / generic-catcher.js
Created November 5, 2018 00:12
Useful error handling of promises and catches
const handle = promise => promise
.then(res => [null, res])
.catch(err => [err, null]);
@csharpforevermore
csharpforevermore / CompareSets.cs
Created October 10, 2018 15:59
Compare two sets of data where I want to confirm ALL elements in one subset can be found in the other superset
var subset = new[] { 2, 4, 6, 8 };
var superset = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// (when comparing small sets, consider O(n*m) solution:)
public static bool ContainsAll<T>(this IEnumerable<T> source, IEnumerable<T> values)
{
bool contained = !subset.Except(superset).Any
return contained;
using System;
using System.Collections.Generic;
namespace Next.Whs.DDespatch.Framework.Models.Mapping.Custom
{
public class ExampleImplementationClass
{
public ExampleImplementationClass()
{
MapperBase<Entity, DTO> userMapper = new UserMapper();
@csharpforevermore
csharpforevermore / GetAllitemsFromListInt.cs
Created September 13, 2018 14:21
Using Linq, get all item Titles from a list by their IDs that existing in another List<int>
var idsOnly = new List<int>();
var myList = db.Items.Where(item => idsOnly.Contains(item.ID.Value))
.Select(a => a.Title).ToList();
@csharpforevermore
csharpforevermore / AssertException.cs
Created September 13, 2018 14:17
Use Assert to verify that an exception has been thrown?
[TestMethod]
[ExpectedException(typeof(ArgumentException),
"A userId of null was inappropriately allowed.")]
public void NullUserIdInConstructor()
{
LogonInfo logonInfo = new LogonInfo(null, "P@ss0word");
}
@csharpforevermore
csharpforevermore / AssertException.cs
Created September 13, 2018 14:17
Use Assert to verify that an exception has been thrown?
[TestMethod]
[ExpectedException(typeof(ArgumentException),
"A userId of null was inappropriately allowed.")]
public void NullUserIdInConstructor()
{
LogonInfo logonInfo = new LogonInfo(null, "P@ss0word");
}
@csharpforevermore
csharpforevermore / GetCallingMethod.cs
Last active February 11, 2019 11:25
Get calling method name in any C# class.
string name = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name;
Console.WriteLine(name);
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- Configurations that affect the Test Framework -->
<RunConfiguration>
<MaxCpuCount>1</MaxCpuCount>
<!-- Path relative to solution directory -->
<ResultsDirectory>.\TestResults</ResultsDirectory>
<!-- x86 or x64 -->
<!-- You can also change it from menu Test > Test Settings > Default Processor Architecture -->
@csharpforevermore
csharpforevermore / gethashtable.cs
Created September 11, 2018 14:16
Retrieve all keys in a HashTable
string keys = string.Join(",", table.Keys.Cast<object>()
.Select(x => x.ToString())
.ToArray());
@csharpforevermore
csharpforevermore / Umbraco7-10GetMediaItem.cs
Created July 2, 2018 19:52
Get a Media item by GUID / ID from a controller
IMedia content = UmbracoContext.Current.Application.Services.MediaService.GetById(Guid.Parse("bc6de686-25fa-49fa-a46b-a2a19c70bd8b"));