Skip to content

Instantly share code, notes, and snippets.

View dejanstojanovic's full-sized avatar
🎧
Focusing

Dejan Stojanovic dejanstojanovic

🎧
Focusing
View GitHub Profile
@dejanstojanovic
dejanstojanovic / String.ConvertTo
Created February 3, 2015 04:32
Convert string value to C# strong type
/// <summary>
/// Converting the string value to T type
/// </summary>
/// <typeparam name="T">Type to convert string value to</typeparam>
/// <param name="value">String value to be converted to type T</param>
/// <returns>Returns converted string value to type T</returns>
public static T ConvertTo<T>(this string value) where T : IConvertible
{
var typeCode = default(T).GetTypeCode();
switch (typeCode)
@dejanstojanovic
dejanstojanovic / RandomEnumerable
Created February 6, 2015 18:12
Returning random element of any list of type T
public static class ExtensionMethods
{
public static Random random = new Random();
public static T Random<T>(this IEnumerable<T> list)
{
return list.ToArray().Random();
}
public static T Random<T>(this T[] array)
{
@dejanstojanovic
dejanstojanovic / DataAccessLayer
Last active August 29, 2015 14:15
Simple DataAccess layer base class which maps value from database to generic type POCO class instance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using log4net;
@dejanstojanovic
dejanstojanovic / ImageSize
Created February 18, 2015 15:06
Fast and short way to get image size in C#
using(var img = Image.FromFile(file.Name))
{
var height = img.Height;
var width = img.Width;
}
@dejanstojanovic
dejanstojanovic / GetProcedureCode
Created February 18, 2015 15:58
SELECT stored procedure code in Microsoft SQL Server
SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME='spDateNow'
@dejanstojanovic
dejanstojanovic / Case insensitive SELECT
Created February 20, 2015 09:40
Perform case insensitive SELECT in Microsoft SQL Server
SELECT * FROM myTable WHERE myField = 'sOmeVal' COLLATE SQL_Latin1_General_CP1_CI_AS
/// <summary>
/// Compares elements of two IEnumerables of same type regardless of element order in both Ienumrables
/// </summary>
/// <typeparam name="T">Type of IEnumerables to compare</typeparam>
/// <param name="Compare">IEnumareble to compare with</param>
/// <param name="CompareTo">IEnumerable to compare to</param>
/// <returns></returns>
public static bool Equals<T>(this IEnumerable<T> Compare, IEnumerable<T> CompareTo)
{
return new HashSet<T>(Compare ?? new List<T>()).SetEquals(CompareTo ?? new List<T>());
SELECT * FROM information_schema.parameters
WHERE specific_name='<stored_procedure_name>'
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE MigrateStoredProcedure
@ProcedureName VARCHAR(50),
@TargetDatabase VARCHAR(50)
AS
BEGIN
public static Type GetClrType(SqlDbType sqlType)
{
switch (sqlType)
{
case SqlDbType.BigInt:
return typeof(long?);
case SqlDbType.Binary:
case SqlDbType.Image:
case SqlDbType.Timestamp: