Skip to content

Instantly share code, notes, and snippets.

string[] allItems = { "item1", "item2", "item3" };
string[] databaseItems = { "item2" };
var data = allItems
.SelectMany(item => databaseItems , (item, dbItem) => new { item, dbItem })
.Where(x => !x.item.Equals(x.dbItem, StringComparison.InvariantCultureIgnoreCase))
.Select(x => x.item);
foreach (var v in data)
Console.WriteLine(v.ToString());
@dotnetdude
dotnetdude / gist:2508250
Created April 27, 2012 10:33
Csharp: Input 10+20+30+40 Output Total
/// Input is a string with numbers : 10+20+30+40
/// Output is integer with required sum (100)
string input = "10+20+30+40";
var result = Regex.Split(input, @"\D+").Select(t => int.Parse(t)).Sum();
Console.WriteLine("Result is {0}" ,result);
@dotnetdude
dotnetdude / gist:2508238
Created April 27, 2012 10:31
Csharp: Create Comma separated string from a string list
var temp = String.Join(", ", myStringList)
@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());
@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: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: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: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:2508099
Created April 27, 2012 10:06
Csharp: Print all items in a list
list.ForEach(Console.WriteLine);
@dotnetdude
dotnetdude / gist:2508091
Created April 27, 2012 10:05
Csharp: Remove duplicate items from a list
var listWithoutDuplicates = list.Distinct().ToList();