Skip to content

Instantly share code, notes, and snippets.

View jasmin-mistry's full-sized avatar
🏠
Working from home

Jasmin Mistry jasmin-mistry

🏠
Working from home
  • London, United Kingdom
View GitHub Profile
public static IEnumerable<TResult> LeftJoin<TSource, TInner, TKey, TResult>(this IEnumerable<TSource> source,
IEnumerable<TInner> inner,
Func<TSource, TKey> pk,
Func<TInner, TKey> fk,
Func<TSource, TInner, TResult> result)
{
IEnumerable<TResult> _result = Enumerable.Empty<TResult>();
_result = from s in source
join i in inner
public static void ConsoleWrite(ConsoleColor color, string text)
{
ConsoleColor originalColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write(text);
Console.ForegroundColor = originalColor;
}
public static void ConsoleWriteLine(ConsoleColor color, string text)
{
public static IEnumerable<DateTime> GetDateRangeTo(this DateTime self, DateTime toDate)
{
var range = Enumerable.Range(0, new TimeSpan(toDate.Ticks - self.Ticks).Days);
return from p in range select self.Date.AddDays(p);
}
public static Dictionary<TFirstKey, Dictionary<TSecondKey, TValue>> Pivot<TSource, TFirstKey, TSecondKey, TValue>(this IEnumerable<TSource> source, Func<TSource, TFirstKey> firstKeySelector, Func<TSource, TSecondKey> secondKeySelector, Func<IEnumerable<TSource>, TValue> aggregate)
{
var retVal = new Dictionary<TFirstKey, Dictionary<TSecondKey, TValue>>();
var l = source.ToLookup(firstKeySelector);
foreach (var item in l)
{
var dict = new Dictionary<TSecondKey, TValue>();
retVal.Add(item.Key, dict);
var subdict = item.ToLookup(secondKeySelector);
public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> collection, int batchSize)
{
List<T> nextbatch = new List<T>(batchSize);
foreach (T item in collection)
{
nextbatch.Add(item);
if (nextbatch.Count == batchSize)
{
yield return nextbatch;
nextbatch = new List<T>(batchSize);
public static partial class CheckBoxList
{
#region String Expression
public static MvcHtmlString CheckBoxListItem(this HtmlHelper htmlHelper, string name, string value)
{
return CheckBoxListItem(htmlHelper, name, value, false, (object)null /* htmlAttributes */);
}
public static partial class MyEnumDropDownList
{
#region Enum DropDown
public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TEnum>> expression)
{
return EnumDropDownListFor(htmlHelper, expression, null);
}
public static partial class MyEnumRadioButtonList
{
#region Enum Radio Button List
public static MvcHtmlString EnumRadioButtonListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TEnum>> expression)
{
return EnumRadioButtonListFor(htmlHelper, expression, null);
}
public static class JSONHelper
{
public static string ToJSON(this object obj)
{
TimeZoneInfo tzi = TimeZoneInfo.Local;
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj.SpecifyDateTimeKind());
}
using System.Text.RegularExpressions;
public static class EMailValidator
{
static Regex ValidEmailRegex = CreateValidEmailRegex();
private static Regex CreateValidEmailRegex()
{
string validEmailPattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
+ @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
+ @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";