Category Archives: Linq

Linq gotcha

One of the great checks when using c# is that it knows the difference between assignment and equality. If you write if a=b the compiler says "no". So with a false sense of security I accidentally wrote some linq along … Continue reading

Posted in Linq | Leave a comment

Linq recipe #3, flatten a dictionary of byte arrays into a single byte array

Dictionary<int, byte[]> binaryParts = new Dictionary<int, byte[]>(); … // now combine the binary from all the parts by first sorting by key var flattenedList = binaryParts.OrderBy(p => p.Key).SelectMany(p => p.Value); byte[] combinedBytes = flattenedList.ToArray();

Posted in Linq | Leave a comment

Linq recipe #2, Convert a CSV string of numbers into an array of numbers

e.g. for every (n) item in the array, apply the Convert function to it and return that as the nth item in the new array   int[] strongValues = Array.ConvertAll(csvString.Split(‘,’), n => Convert.ToInt32(n));

Posted in Linq | Leave a comment

Linq recipe #1 combining two enums into a single dictionary

// Using Linq, ignore problems with jaggies List<int> keys = new List<int> { 1, 2, 3, 4, 5 };            List<string> data = new List<string> { "a", "b", "c", "d","e" };   Dictionary<int,string> combo = (from k in keys             join … Continue reading

Posted in Linq | Leave a comment