Posts Tagged Microsoft Developer Network

LINQ Short Takes – Number 2 – Using Method Syntax to Create a Cartesian Product


This is the second post in a series (I have no idea how many blog posts this series may comprise) of LINQ Short Takes. The first post was LINQ Short Takes – Number 1 – Enumerable.Range() which presented the Enumerable.Range() method. This blog post utilises the Enumerable.Range() method to form a Cartesian product utilising the LINQ method syntax (see: MSDN (Microsoft Developer Network Web Site) Article – LINQ Query Syntax versus Method Syntax (C#)for further discussion of the topic).

The motivation for developing this approach was twofold:

1. I did need a sequence of integers that formed a Cartesian product.

2. I experienced a degree of exasperation, frustration and downright ‘that’s wrong and very tacky’ when I looked for LINQ Cartesian Products using Method syntax. The solutions I read used a SelectMany()and lacked clarity that any good code should have.

So, I decided that there must be a simpler way to form a Cartesian product from two sequences using LINQ method calls. What has resulted is a solution to the problem that, in my opinion, is clear and concise. The solutions implementation uses a LINQ Join() method call to build the Cartesian product.

Cartesian Products

Please excuse the diversion into a little bit set theory. This diversion explains why the LINQ Join() method succeeds in forming a Cartesian product.

A Cartesian productis a resulting sequence of two input sequences. The output sequence has that all elements in the first sequence join with each element of the second sequence. The resulting sequence is series of element pairs from both input sequences. The length of the output sequence is the product of the lengths of the two input sequences.

The join operation between the two sequences that form the Cartesian producthas the following properties. If all of the elements in the two input sequences match each other, we have a type of join with a special property. This type of join’s special property implies that the join key for all of the elements in the two sequences is the same value.

Armed with the conclusion that a Cartesian product has the join key for both inputs sequences should be the same value. The process of forming a C# statement which utilises the Join() method to form a Cartesian product between the two input sequences becomes a simple process.

Example Code

The following C# method contains three examples of forming a Cartesian product. I will comment a little more on the three examples after the code.

The code also utilises the LINQ extension method ToOutput. This was the subject of a previous blog post: Dumping a formatted IEnumerable to Output.

I have loaded this code in Work docx and pfd formats as well. The URL’s are:

private void CartesianProducts()
    // Query Syntax Cartesian Product var Prod1 = from num1 in Enumerable.Range(0, 10)
                from num2 in Enumerable.Range(0, 10)
                select new { num1, num2 };
    Debug.WriteLine("/nDump of Prod1");
    // Dump the contents of Prod1 Prod1.ToOutput(FormatFunction:
        (objValue, Position) =>
            return string.Format("[{0}] {1},{2}\n",
                Position, objValue.num1, objValue.num2);

    // Generate a Cartesian product using LINQ method syntax from two sequences var Prod2 = Enumerable.Range(0, 10).Join(Enumerable.Range(0,10),
        (iVal1) => 1, // Key selectors make all elements in first sequence (iVal2) => 1, // match all elements in the second sequence. (iVal1, iVal2) =>
            new {iVal1, iVal2}); // Create an object with the two values. Debug.WriteLine("/nDump of Prod2");
    // Dump the contents of Prod2 Prod2.ToOutput(FormatFunction:
        (objValue, Position) =>
            return string.Format("[{0}] {1},{2}\n",
                Position, objValue.iVal1, objValue.iVal2);

    string[] SampleStrings = new string[]
    { "The", "quick", "red", "fox", "jumped",
        "over", "the", "lazy", "brown", "cow" };
    // Generate a 3 way Cartesian product var Prod3 = Enumerable.Range(0, 10)
                    .Join(Enumerable.Range(0, 5), iVal1 => 1, iVal2 => 1,
                        (iVal1, iVal2) => new { iVal1, iVal2 })
                    .Join(SampleStrings, iVals => 1, strVals => 1,
                        (iVals, strVal) =>
                            new {
                                intValue1 = iVals.iVal1,
                                intValue2 = iVals.iVal2,
                                strValue = strVal });
    Debug.WriteLine("/nDump of Prod3");
    // Dump the contents of Prod3 Prod3.ToOutput(FormatFunction:
        (objVal, Position) =>
            string.Format("[0] {1},{2},'{3}'\n",
                Position, objVal.intValue1, objVal.intValue2, objVal.strValue));
        "Double Check\n\tSequence1 Length {0} Sequence2 Length {1}" +
        "SampleStrings Length{2}\n\t" +
        "Input Sequence Product Value {3} Result Length {4}",
        Enumerable.Range(0, 10).Count(),
        Enumerable.Range(0, 5).Count(),
        Enumerable.Range(0, 10).Count()
            * Enumerable.Range(0, 5).Count()
            * SampleStrings.Count(),


The examples of forming a Cartesian product with LINQ are:

  1. This example uses the LINQ query syntax to create a Cartesian product. It uses an unconstrained join between the two sequences. This is exactly how SQL operates when used to form a Cartesian product.
  2. The second example uses the LINQ method syntax to form a Cartesian product between two integer sequences. These sequences are generated using the Enumerable.Range () method. The Cartesian product is formed using the LINQ method Join(). The arguments to the overload of the Join() methods are: the second IEnumerable, the outer (first sequence) key selector, the inner (the second sequence) key selector, and the result selector. The key selectors in each part of the join are just 1, which ‘says’ this is the join key for each of the same sequences.
  3. The third example demonstrates the scalability of the approach, by joining three sequences in a Cartesian product. If you can do this with two, and three sequences, then I would expect the approach would work with as many sequences as you require. This example also demonstrates the joining of different types of object using this approach.

If you missed it above, this example method also uses the ToOutput LINQ extension method. The ToOutput extension method was the subject of a previous blog post: Dumping a formatted IEnumerable to Output, and the code for the extension method is included there.


  • I believe that the approach presented is a clear, clean, and concise, approach to generation of a Cartesian product using the LINQ method syntax.
  • I suspect that I will generalise this approach into another LINQ extension method. The may well become another blog post. There are a couple of interesting deign decisions which I need to make when designing and implementing this approach as an extension method. I will leave those discussions for that blog post.

, , , , , , , ,


Compiled Help File (chm) Error – Navigation to the webpage was canceled


This error caught me today at work. I was trying to read up on TFS (Team Foundation Server), and downloaded a couple of chm (Compiled Help files) from the MSDN site.


Trying to open a compiled help file (chm) which I downloaded from the MSDN site, get the following error:


The Problemclip_image004

Windows 7 is being a bit overprotective. Although I say yes I want to open the file:


You still need to do one more thing. Which is unblock the file. clip_image006



It may be a useful feature, for some people. It may be my work’s network which has the security levels cranked up to a very high level. For me it is a right pain. Particularly, when I’ve already said yes to one thing only to find I need to do something more.

, , , , , , ,


Multithreaded Programming in .Net with C# – Aggregated Exceptions


This may seem like an odd topic for inclusion in my multithreaded in C#, with the .Net 4.0 framework. If you read on you will see why this is  a topic for consideration when programming using the .Net 4.0 Framework. When you see why the designers of the .Net Framework included the AggregateException Class you will see that there is a use case here which needed to be addressed.


My previous blog post in this series was: Parallel Programming in .Net – Resources.

The Prompt For this Blog Post

Another resource:   Aggregating Exceptions or

This is an interesting article on the AggregateException Class. This class is heavily used in the .Net Framework parallel support classes. Why? Read on for some explanations as to why the .Net Framework needed aggregated exceptions.

Single Threaded Exception Model

In single the single threaded programming model, one thread can only throw one exception at a time.

The following is a very simple example of exception processing in a single threaded environment. What it does show is that only one exception can be “active” at once.

private void method_a()
    catch (NotImplementedException ex)

private void method_b()
    throw new NotImplementedException();

Multiple Threaded Exception Model

In the multiple threading model you can potentially have multiple exceptions being thrown (one from each thread executing).

The following is a very simple example. There are a couple of point to note:

  • I don’t know how many exceptions will be thrown.
  • The number of exceptions depends on a number of factors wedged into the Parallel.For method.
    • How many CPU’s (or hardware threads) the machine has. The implementation scales out to what the machine has.
    • The implementation could called from another Parallel.For (or any other Framework Parallel method – including Parallel LINQ), so again the number of threads available is unknown.
    • The implementation has it’s own scale across algorithms. So again depending on the way the algorithm reacts to the current workload, we could get any number of threads.
private void root_Method()
    Parallel.For(1, 20, a=> {
        throw new NotImplementedException(a.ToString());

There are a number of things which should be made clear. These are:

  1. We don’t know how many exceptions the caller should get.
  2. The ordinary exception cannot cater for more than one “meaning”. The Exception Class has an (just one) inner exception only.
  3. How does the implementation of Parallel.For decide which exception it should pass back from an arbitrary delegate (or, any code you like).

Why We Need Aggregated Exceptions?

The above starts to outline the use case for an aggregated exception, or more to the point an exception which can contain multiple exceptions. When you have multiple threads of execution each of those threads could throw an exception. These exceptions should go back to the thread which propagated the multiple threads. The caller should be “told” about all of the problems which occurred. As a result the .Net Framework “packs” all of these exceptions which occurred in the parallel operations into one exception which is passed (thrown) back to the caller. Acting as a container for all exceptions generated in a function is the function which the AggregateException Class performs.

The Structure of the AggregateException Class Class

The following code snippet is lifted from the MSDN article Aggregating Exceptions. I have included the code here so that I can highlight some of the additions that the AggregateException Class has, when compared to the “normal” exception classes.

[DebuggerDisplay("Count = {InnerExceptions.Count}")]
public class AggregateException : Exception
    public AggregateException();
    public AggregateException(params Exception[] innerExceptions);
    public AggregateException(IEnumerable<Exception> innerExceptions);
    public AggregateException(string message);
    public AggregateException(string message, Exception innerException);
    public AggregateException(string message,
        params Exception[] innerExceptions);
    public AggregateException(string message,
        IEnumerable<Exception> innerExceptions);

    public AggregateException Flatten();
    public void Handle(Func<Exception, bool> predicate);

    public ReadOnlyCollection<Exception> InnerExceptions { get; }

AggregateException.InnerExceptions Propertyimage

This is where the exceptions from the multiple threads are stored. There is a subtle nuance in this storage structure, which is that it is a hierarchical representation of the exceptions thrown.

The following diagram attempts to show that one AggregatedException can contain other Aggregated Exceptions, and ordinary exceptions. The .Net Framework will generate another “level” (insert another aggregated exception) when there is another level of the parallel support in the Framework is invoked. For example, a Parallel.For which invokes a child Parallel.For. These parent child relationships are preserved in the aggregated exception structure which the Framework generates.

AggregateException Constructor

It is worth noting that the constructors for the AggregatedException include the ability to construct an AggregatedException from collections of exceptions. As you would expect, when building a container object.

AggregateException.Flatten Method

This is an interesting method which has been included into the implementation of the AggregatedException. It simply flattens the hierarchal, or nested, structure which the AggregatedException contains into a single level representation. This representation is then ideal for applying LINQ to the inner exceptions collection, and “pulling” out the exceptions you are interested in.

The following is a non functional piece of code, but demonstrates the use of the Flatten method with LINQ.

AggregateException sample = new AggregateException();
var interesting_exceptions = from ex in sample.Flatten().InnerExceptions
                             where ex is DivideByZeroException
                             select ex;

AggregateException.Handle Method

The handle method is another way of working with the AggregatedException an the collection of exceptions which it can contain. This method produces an aggregated exception which contains just the exceptions which do not satisfy the predicate supplied.

    sample.Flatten().Handle(e => e is DivideByZeroException);
catch (AggregateException ex)
    // Sample minus the DivideByZeroExceptions


I hope that this discussion of the AggregateException Class proves useful.

It is a part of the .Net 4.0 Framework which undoubtable any developer producing multithreaded code will need to deal with.

, , , , , , , , ,

Leave a comment

%d bloggers like this: