Skip to content

Instantly share code, notes, and snippets.

View develohpanda's full-sized avatar
✌️
Chillin'

Opender Singh develohpanda

✌️
Chillin'
View GitHub Profile
@develohpanda
develohpanda / WeightedRandomSelection.cs
Last active April 7, 2020 16:34
Weighted Random Selection - a simplified implementation of the solution found at http://programmers.stackexchange.com/a/150642/244423
/// <summary>
/// Selects a random element from input list using the weights on T to sway selection.
/// T must have a Weight property on it.
/// </summary>
public T SelectRandom<T>(List<T> objects)
{
int totalWeight = 0;
T selected = default(T);
foreach (T obj in objects)
{
@develohpanda
develohpanda / WeightedRandomSelectionWithLimitedAllocations.cs
Last active April 7, 2020 16:33
Weighted Random Selection With Limited Allocations - a modified implementation of the solution found at http://programmers.stackexchange.com/a/150642/244423
/// <summary>
/// Selects a random element from input list using the allocations remaining to sway selection
/// T must have AllocationRemaining, Weight and Id properties on it.
/// </summary>
public T SelectRandom<T>(List<T> objects)
{
int totalWeight = 0;
T selected = default(T);
foreach (T obj in objects)
{
@develohpanda
develohpanda / RoundToNearestXSeconds.txt
Created September 19, 2016 01:27
Power BI - Round to nearest x seconds
DateTime.From(Number.Round((Number.From([Time]) * 24 * 3600)/RoundToNearestXSecond, 0)*RoundToNearestXSecond/(24*3600))
@develohpanda
develohpanda / RunSync.cs
Created October 8, 2016 15:18
Run methods returning a Task synchronously, and return any output values. YOU SHOULD NEVER, EVER HAVE TO DO THIS, but if the need arises in that one rogue case....
public static class AsyncHelpers
{
/// <summary>
/// Execute an async method which has a void return value synchronously
/// </summary>
/// <param name="method">Task<T/> method to execute</param>
public static void RunSync(Func<Task> method)
{
Argument.CheckIfNull(method, nameof(method));
@develohpanda
develohpanda / FixDb.sql
Created April 27, 2017 01:51
Entity Framework reverse engineering making VS hang
UPDATE STATISTICS sys.syscolpars
UPDATE STATISTICS sys.sysschobjs
UPDATE STATISTICS sys.syssingleobjrefs
UPDATE STATISTICS sys.sysiscols
GO
@develohpanda
develohpanda / convert-UNIX-timestamp.js
Created May 21, 2017 09:54 — forked from kmaida/convert-UNIX-timestamp.js
Convert a UNIX timestamp to user's local time via JavaScript
function convertTimestamp(timestamp) {
var d = new Date(timestamp * 1000), // Convert the passed timestamp to milliseconds
yyyy = d.getFullYear(),
mm = ('0' + (d.getMonth() + 1)).slice(-2), // Months are zero based. Add leading 0.
dd = ('0' + d.getDate()).slice(-2), // Add leading 0.
hh = d.getHours(),
h = hh,
min = ('0' + d.getMinutes()).slice(-2), // Add leading 0.
ampm = 'AM',
time;
@develohpanda
develohpanda / delete-sql.ps1
Last active May 24, 2022 15:38
Drop SQL database from Powershell
function Delete-SqlDatabase($serverName, $databaseName) {
Import-Module SQLPS
$server = New-Object Microsoft.SqlServer.Management.Smo.Server($serverName)
$db = $server.databases[$databaseName]
if ($db) {
$server.KillAllprocesses($databaseName)
$db.Drop()
}
}
@develohpanda
develohpanda / run-sqlcommand.ps1
Created July 4, 2017 04:40
Run SQL from Powershell
function Run-SqlCommand($serverName, $databaseName, $command, $timeoutSeconds = 30) {
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$serverName;Database=$databaseName;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $command
$SqlCmd.Connection = $SqlConnection
$SqlCmd.CommandTimeout = $timeoutSeconds
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
@develohpanda
develohpanda / update-rows-using-join.sql
Created July 11, 2017 03:43
Update Rows using join
update ft
set ft.description = 'test'
from firstTable ft
inner join secondTable st on
ft.id = st.id
@develohpanda
develohpanda / DeleteRowsAfterJoinCondition.sql
Created July 13, 2017 03:59
Delete after a join condition
DROP TABLE IF EXISTS #toDelete
SELECT bmi.* INTO #toDelete
FROM [dbo].[BCS_Main_Input_With_absence_Holiday_And_Wage_Elements] bmi
INNER JOIN @fiscalWeeksForRelease fw
on
fw.FISCAL_YEAR = bmi.Fiscal_Year
AND fw.FISCAL_WEEK = bmi.Fiscal_Week
INNER JOIN @StoresInRelease s
on
s.Store_Id = bmi.Store_ID