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 / ShowModelProperties.cshtml
Created September 13, 2019 00:31
Umbraco - list properties for current Model
foreach (var prop in Model.Properties)
{
<p>@prop.Alias + " - " + @prop.Value()</p>
}
SELECT STRAIGHT_JOIN categories.id, categories.title, categories.game_count, categories.display_order FROM `categories` INNER JOIN `category_namespaces` ON `category_namespaces`.`category_id` = `categories`.`id` WHERE `categories`.`type` IN ('ClickJogos::GameCategory') AND `categories`.`published` = 1 AND (category_namespaces.namespace = 'jgo') ORDER BY
CASE
WHEN categories.id LIKE '447%' THEN 1
WHEN categories.id LIKE '448%' THEN 2
ELSE 3
END, categories.game_count DESC;
@csharpforevermore
csharpforevermore / CountLinesInTextFile.cs
Created March 11, 2019 16:49
Counts the number of lines in a text file
// 4.0
var lineCount = File.ReadLines(@"C:\file.txt").Count();
// pre-4.0
var lineCount = File.ReadAllLines(@"C:\file.txt").Length;
// pre-4.0 (more efficient)
var lineCount = 0;
@csharpforevermore
csharpforevermore / ExtensionMethod.cs
Created February 28, 2019 11:45
Nicer strongly-typed way to get a value from a web.config or app.config file. Sourced from http://byatool.com/uncategorized/configurationmanager-and-appsettings-a-wrapper-class-story/
protected static T? GetValueFromConfiguration<T>(String key) where T : struct
{
T? returnValue = null;
if (System.Configuration.ConfigurationManager.AppSettings[key] != null)
{
returnValue = System.Configuration.ConfigurationManager.AppSettings[key]
.ConvertTo<T>(); //MY AWSOME CONVERTTO METHOD!!11
}
return returnValue;
@csharpforevermore
csharpforevermore / IsIdentityInsertOn.sql
Last active February 6, 2019 14:24
Search all stored procedures for text string 'szUserName'
-- https://stackoverflow.com/questions/10637976/how-do-you-check-if-identity-insert-is-set-to-on-or-off-in-sql-server
SELECT OBJECTPROPERTY(OBJECT_ID('MyTable'), 'TableHasIdentity');
-- when using an API wrapper, one can reduce the entire check to just checking for rows. For instance when using C#'s SqlDataReaders property HasRows and a query construct like:
SELECT CASE OBJECTPROPERTY(OBJECT_ID('MyTable'), 'TableHasIdentity') WHEN 1 THEN '1' ELSE NULL END
-- more flexibility but needs column name
SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('MyTable', 'U') AND name = 'MyTableIdentityColumnName';
@csharpforevermore
csharpforevermore / SearchAllText.sql
Created February 6, 2019 09:17
Search all test for SQL string
CREATE PROC SearchAllTables
(
@SearchStr nvarchar(100)
)
AS
BEGIN
CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
@csharpforevermore
csharpforevermore / ContainsAll.cs
Last active December 27, 2018 04:30
Does A contain all items of B? When A = actual, B = expected. Both are lists because they implement IEnumerable and so work with LINQ
class Code {
public static bool ContainsAll<T>(this IEnumerable<T> actual, IEnumerable<T> expected) {
return actual.All(a => expected.Contains(a));
}
}
@csharpforevermore
csharpforevermore / GeneralCommands.ps1
Created December 24, 2018 15:18
A collection of PowerShell commands
# List Current Version
$PSVersionTable.PSVersion
Example 1: Send output to a file
Get-Process | Out-File -filepath C:\Test1\process.txt
# Example 2: How to retrieve recursively any files with a specific extensions in PowerShell?
# https://stackoverflow.com/questions/31049454/how-to-retrieve-recursively-any-files-with-a-specific-extensions-in-powershell
@csharpforevermore
csharpforevermore / example1.cs
Created November 15, 2018 03:51
An example of the keywords virtual and sealed
public class ClassA
{
public virtual void Method1() { Console.WriteLine("ClassA"); }
}
public class ClassB : ClassA
{
public override sealed void Method1() { Console.WriteLine("ClassB"); }
public void Method2() { Console.WriteLine("ClassB"); }
}