Skip to content

Instantly share code, notes, and snippets.

View angularsen's full-sized avatar

Andreas Gullberg Larsen angularsen

View GitHub Profile
@angularsen
angularsen / gist:ed5d5af4967b25ddb904
Last active August 29, 2015 14:08
Simpler forms
// CreateEdit.cshtml
@using (Html.BeginForm("Create", "Licenses", FormMethod.Post, new {role = "form"}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new {@class = "text-danger"})
@Html.HiddenFor(m => m.App.Id)
<fieldset>
<legend>Constraints</legend>
@Html.EditorFor(m => m.Constraints.MaxInitialReleaseDateOfMinorVersion, "TextBox")
public static class TaskExtensions
{
public static async void ForgetAsync(this Task task)
{
try
{
await task.ConfigureAwait(false);
}
catch
{
@angularsen
angularsen / MarginSetter.cs
Created January 17, 2015 12:19
WPF - Automatic horizontal or vertical spacing in StackPanel and other list like panels
using System.Windows;
using System.Windows.Controls;
using JetBrains.Annotations;
namespace Foo
{
public class MarginSetter
{
private static Thickness GetLastItemMargin(Panel obj)
{
@angularsen
angularsen / FooController.cs
Last active August 29, 2015 14:14
Update pattern for MVC + Dal
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(EditFooVm vm)
{
if (!ModelState.IsValid)
{
SaveModelState();
return RedirectToAction("Edit", new {id = vm.FooId});
}
@angularsen
angularsen / NameValueCollectionExtensions.cs
Created March 19, 2016 11:11
Get web/app.config setting and expand environment variables when developing locally
using System;
using System.Collections.Specialized;
using JetBrains.Annotations;
namespace MyExtensions
{
public static class NameValueCollectionExtensions
{
[CanBeNull]
public static string GetWithEnv([NotNull] this NameValueCollection nvc, [NotNull] string name)
@angularsen
angularsen / NameValueCollectionExtensions.cs
Created March 19, 2016 11:16
Get web/app.config setting and expand environment variables when developing locally
using System;
using System.Collections.Specialized;
using JetBrains.Annotations;
namespace MyExtensions
{
public static class NameValueCollectionExtensions
{
[CanBeNull]
public static string GetWithEnv([NotNull] this NameValueCollection nvc, [NotNull] string name)
@angularsen
angularsen / nuget log
Created April 9, 2016 20:31
Loggur.Uwp: Failed to add nuget Microsoft.AspNetCore.Hosting-rc3-20533
Restoring packages for 'Loggur.Uwp'.
Restoring packages for C:\Repos\Loggur\Loggur.Uwp\project.json...
Version conflict detected for System.Collections.
Loggur.Uwp (>= 1.0.0) -> Microsoft.AspNetCore.Hosting (>= 1.0.0-rc3-20533) -> Microsoft.AspNetCore.Http.Extensions (>= 1.0.0-rc3-20533) -> Microsoft.AspNetCore.Http.Abstractions (>= 1.0.0-rc3-20533) -> System.Reflection.TypeExtensions (>= 4.1.0-rc2-23931) -> System.Linq (>= 4.1.0-rc2-23931) -> System.Collections (>= 4.0.11-rc2-23931)
Loggur.Uwp (>= 1.0.0) -> Microsoft.NETCore.UniversalWindowsPlatform (>= 5.0.0) -> Microsoft.NETCore.Runtime (>= 1.0.0) -> Microsoft.NETCore.Runtime.CoreCLR-x64 (>= 1.0.0) -> System.Collections (= 4.0.10).
System.Console 4.0.0-rc2-23931 provides a compile-time reference assembly for System.Console on UAP,Version=v10.0, but there is no run-time assembly compatible with win10-x64.
System.Reflection.Extensions 4.0.1-rc2-23931 provides a compile-time reference assembly for System.Reflection.Extensions on UAP,Version=v10.0, but the
@angularsen
angularsen / Application_Start.cs
Last active November 5, 2019 09:08
CreateOrMigrateDatabase()
// Call me on startup, such as in Application_Start() of Global.asax.cs
private static void CreateOrMigrateDatabase()
{
// Disable automatic migrations as it prevents us from deploying DB changes without breaking the production web instance.
Database.SetInitializer<ApplicationDbContext>(null);
var migrator = new DbMigrator(new Configuration());
// Order by migration names to get oldest first, such as 201403221133523_AddSetting
List<string> pendingMigrations = migrator.GetPendingMigrations().OrderBy(x => x).ToList();
@angularsen
angularsen / PropertyPath.cs
Last active December 17, 2016 11:40
Helper class for converting property expression x => x.Foo.Bar to property path string "Foo.Bar"
/// <remarks>Inspired by: http://stackoverflow.com/a/22135756/134761 </remarks>
public static class PropertyPath<TSource>
{
public static string GetString(Expression<Func<TSource, object>> expression, string separator = ".")
{
return string.Join(separator, GetPropertyPathSegments(expression));
}
public static IReadOnlyList<string> GetPropertyPathSegments(Expression<Func<TSource, object>> expression)
{
@angularsen
angularsen / unitsnet-equations.linq
Created January 20, 2017 21:42
LINQpad example for dynamically converting source quantity strings to other units
void Main()
{
ElectricCurrent sourceQuantity = ElectricCurrent.Parse("8A");
ElectricCurrentUnit milliAmpsUnit = ElectricCurrent.ParseUnit("mA");
// 8000
double milliAmps = sourceQuantity.As(milliAmpsUnit);
// 8,000 mA (current culture, happens to be US English on my Windows)
Console.WriteLine(sourceQuantity.ToString(milliAmpsUnit));