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 the lines of;

var x = from a in somelist where a.IsRead=true select a

yep you guessed it actually executed and carried out the assignment rather than evaluating the WHERE. So keep em peeled

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();

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 d in data on keys.FindIndex(x => x==k) equals data.FindIndex(x2 => x2==d)                           

            select new {Key=k, Value=d} ).ToDictionary(de=>de.Key, de=>de.Value);

foreach (var i in combo)


    System.Diagnostics.Debug.WriteLine(i.Key + "," + i.Value);



// Using lamda expression, doesn’t allow jaggies

Dictionary<int, string> UrisByProject = new Dictionary<int, string>();

List<int> nonZeroProjectIds = new List<int>() { 1, 2, 3, 4,5 };

List<string> urisForTheImage = new List<string>() { "a", "b", "c", "d", "e" };

nonZeroProjectIds.ForEach(p => UrisByProject.Add(p, urisForTheImage[nonZeroProjectIds.FindIndex(t => t == p)]));

foreach (var i2 in UrisByProject)


    System.Diagnostics.Debug.WriteLine(i2.Key + "," + i2.Value);