Skip to content

Instantly share code, notes, and snippets.

View afreeland's full-sized avatar

Aaron Freeland afreeland

View GitHub Profile
@afreeland
afreeland / gist:6174911
Created August 7, 2013 15:09
JavaScript: Basic Sort Function
// Sorts array elements in ascending order numerically.
function CompareForSort(first, second)
{
if (first == second)
return 0;
if (first < second)
return -1;
else
return 1;
}
@afreeland
afreeland / gist:6390966
Created August 30, 2013 15:21
JavaScript: Execute function via namespace
/**
* Executes a function by simply providing a namespace
* @param {string} functionName A namespace such as '_n.Roles.Order.SetOrderNotes'
* @param {obj} context The Initial object context to start from, such as window
* @return Returns back the result of whatever the function returns
*/
function executeFunctionByName(functionName, context /*, args */) {
var args = Array.prototype.slice.call(arguments).splice(2);
var namespaces = functionName.split(".");
var func = namespaces.pop();
@afreeland
afreeland / gist:6730973
Last active May 30, 2020 12:16
C#: Create an Expression to use with LINQ
public ActionResult Grid(string FirstName, string LastName)
{
GridHelper.Filters filter = new GridHelper.Filters("FirstName", "LastName");
var _page =(!String.IsNullOrEmpty(HttpContext.Request.QueryString["page"])) ? Convert.ToInt32(HttpContext.Request.QueryString["page"]) : 1;
var _contacts = from c in testDB.Contacts
join ac in testDB.Account_Contact on c.ID equals ac.ContactID
where ac.AccountID == 725
select c;
@afreeland
afreeland / gist:6733381
Last active March 1, 2024 10:32
C#: LINQ Expression Builder dynamic where clause based on filters
public class ExpressionBuilder
{
// Define some of our default filtering options
private static MethodInfo containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
private static MethodInfo startsWithMethod = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
private static MethodInfo endsWithMethod = typeof(string).GetMethod("EndsWith", new[] { typeof(string) });
public static Expression<Func<T, bool>> GetExpression<T>(List<GridHelper.Filter> filters)
{
// No filters passed in #KickIT
@afreeland
afreeland / gist:6734584
Created September 27, 2013 20:19
C#: Invoke Method from MethodInfo
// Define our Methods
private static MethodInfo containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
private static MethodInfo startsWithMethod = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
private static MethodInfo endsWithMethod = typeof(string).GetMethod("EndsWith", new[] { typeof(string) })
// How to use (Invoke) our Method
containsMethod.Invoke("FirstName", new object[]{"First"}); // returns true
@afreeland
afreeland / gist:6796800
Last active August 19, 2022 13:58
C#: Reflection - Get Property Type, Switch on Type, Set Property Value
public static void SetKey<T>(T obj, TempDataDictionary _tempData)
{
System.Type type = typeof(T);
// Get our Foreign Key that we want to maintain
String foreignKey = _tempData["ForeignKey"].ToString();
// If we do not have a Foreign Key, we do not need to set a property
if (String.IsNullOrEmpty(foreignKey))
return;
@afreeland
afreeland / gist:6924723
Created October 10, 2013 20:08
RegEx: Regular Expression to match HTML element and any inside text contained in node for Sublime Text 2
// #Regex Regular Expression to match HTML Element structure containing any
// text beginning/ending with whitespace in Sublime Text 2
<div class="editor-label">(\s*)(.*?)(\s*)</div>
// Matches follwing HTML structure
<div class="editor-label">
@Html.LabelFor(model => model.GlobalCustomer)
</div>
<div class="editor-label">@Html.LabelFor(model => model.GlobalCustomer)</div>
@afreeland
afreeland / gist:8184438
Last active November 11, 2021 11:45
JavaScript: CSV Get Headers from input stream using ArrayBuffer
function CSVImportGetHeaders()
{
// Get our CSV file from upload
var file = document.getElementById('CSVUpload').files[0]
// Instantiate a new FileReader
var reader = new FileReader();
// Read our file to an ArrayBuffer
reader.readAsArrayBuffer(file);
@afreeland
afreeland / gist:8849587
Created February 6, 2014 18:16
RegEx: General regular expression helpers
// Regex \[(.*?)\]
// Result [BinLocation] [nvarchar]
[BinLocation] [nvarchar](20) NOT NULL,
// RegEx \[([^\[]+)$
// \[ matches [
// [^\/[]+] Matches last occurence of [
// $ matches end of line
// Result [nvarchar](20) NOT NULL,
@afreeland
afreeland / gist:9390169
Created March 6, 2014 13:53
SQL: Common table expression to update a segmented query
with cte
as(
select ID, AccountID, ItemTypeID, SourceChannelID, ntile(3) over(order by id) as tile_nr from [Item].[Item] where AccountID = 730
)
update [item].[item]
set SourceChannelID = cte.tile_nr
from [item].[item] ii
inner join cte on ii.ID = cte.ID
where ii.AccountID = 730