Skip to content

Instantly share code, notes, and snippets.

@dotnetdude
dotnetdude / gist:2489484
Created April 25, 2012 12:43
SQL: Find a table column on SQL Server
SELECT name FROM sysobjects WHERE id IN ( SELECT id FROM syscolumns WHERE name = 'email' )
SELECT name FROM sysobjects WHERE id IN ( SELECT id FROM syscolumns WHERE name like '%PART_OF_NAME%' )
@dotnetdude
dotnetdude / gist:2508010
Created April 27, 2012 09:56
Csharp: Filter lists
var list = new List<string>() {"Asia", "Africa", "North America", "South America", "Antartica", "Europe", "Australia"};
// Get all the items from the list that start with
// an 'A' and have 'r' as the 3rd character
var filteredList = list.Where(item => item.StartsWith("A")).Where(item => item[2] == 'u').ToList();
@dotnetdude
dotnetdude / gist:2508048
Created April 27, 2012 09:58
Csharp: Create a new list from the first items of another list
// Take the first 3 items from list 'list' and create a new list with them
var shortList = list.Take(3).ToList();
@dotnetdude
dotnetdude / gist:2508091
Created April 27, 2012 10:05
Csharp: Remove duplicate items from a list
var listWithoutDuplicates = list.Distinct().ToList();
@dotnetdude
dotnetdude / gist:2508099
Created April 27, 2012 10:06
Csharp: Print all items in a list
list.ForEach(Console.WriteLine);
@dotnetdude
dotnetdude / gist:2508110
Created April 27, 2012 10:07
Csharp: Cool string counting stuff
var str = "H1e2l3l4l5o6";
// Count all digits in a string
var numOfDigits = str.Count(char.IsDigit);
// Count all lowercase characters in a string
var numOfLowerCase = str.Count(char.IsLower);
// Count all uppercase characters within a string
var numOfUpperCase = str.Count(char.IsUpper);
@dotnetdude
dotnetdude / gist:2508119
Created April 27, 2012 10:08
Csharp: Comparing two lists
var list = new List<string>() { "Asia", "Africa", "North America", "South America", "Antartica", "Europe", "Australia" };
var list2 = new List<string> {"Africa", "South America", "Antartica", "Foo"};
// Get all items in the list that do NOT have matching items on a different list
var list3 = list.Except(list2).ToList();
// Get all items in the list that have matching items on a different list
list3 = list.Intersect(list2).ToList();
@dotnetdude
dotnetdude / gist:2508166
Created April 27, 2012 10:13
Csharp: Comma Separated numbers into Array
string str = "45,43,122,75,34";
int[] values = str.Split(',').Select(o => Convert.ToInt32(o)).ToArray();
@dotnetdude
dotnetdude / gist:2508195
Created April 27, 2012 10:21
Csharp: Get All Child Categories
return categories.SelectMany(i => this.GetAllChildCategoriesIds(i.Id));
@dotnetdude
dotnetdude / gist:2508214
Created April 27, 2012 10:25
Csharp: Convert list of space separated string into list of numbers and find the maximum
Console.WriteLine(Console.ReadLine().Split(' ').Select(t => int.Parse(t)).ToList().Max());