Setting up a Microsoft Dynamics CRM 2013 Development Server on Windows Azure

In a previous post I referenced instructions for installing BizTalk 2013 on Windows Azure. I also installed Microsoft Dynamics CRM 2013 on a virtual machine on Windows Azure. I found this post on survivingcrm blog to be extremely helpful. Here is a direct link to the slideshare instructions referenced on the blog post.

I did experience a couple of issues as explained here.

1. On slide 46 the CRM Reporting Extensions setup dialog is shown. This dialog did not appear for me so I run the setup manually which is located at c:\Users\crm_admin\SrsDataConnector\Microsoft.Crm.Setup.SrsDataConnector.exe

2. Once the installation was complete and running the site in IE, the site displays with the mobile theme. At the time of writing Microsoft are working on a fix. A work around for now is to ensure you navigate directly to the /main.aspx page on desktop systems.
So, if your CRM URL is http://mysite.com/organisationname then the URL to use would be http://mysite.com/organisationalunit/main.aspx

Another option instead of using Windows Azure is to create a trial on CRM Online as described here.

Setting up a BizTalk 2013 Development Server on Windows Azure

I have recently installed BizTalk 2013 on to a virtual machine on Windows Azure. The following articles on Sandro Pereira Biztalk Blog are a very good resource which I have used a number of times. Listed here to bookmark for future reference.

Using Enumerable.Intersect to find items in two lists

On a recent post where I discussed using LINQ to find all items in one list which also exist in another list (intersection) using linq expressions, a comment was posted suggesting I look into using Enumerable.Intersection.  So this is it.

First of all a straight forward example, finding the intersect of two lists of integers

Console.WriteLine("Number intersect...");
var lista = new List<int> { 1, 44, 56, 87, 23 };
var listb = new List<int> { 20, 23, 1 };
var numbers = lista.Intersect<int>(listb);
numbers.ToList().ForEach(i => Console.WriteLine(i));

The result of the Intersect() method returns two integers because they exist in both lists.

1
23

And a simple example using strings.

Console.WriteLine("String intersect...");
var listx = new List<string> { "Ben", "Phil", "Mary", "Paula", "Peter", "Samuel" };
var listy = new List<string> { "samuel", "Paula", "Ben" };
var names = listx.Intersect<string>(listy, StringComparer.OrdinalIgnoreCase);
names.ToList().ForEach(n => Console.WriteLine(n));

As StringComparer.OrdinalIgnoreCase is being passed as the second parameter to the Intersect() method three matches are being found, if case was not ignored the ‘samuel’ string in listy would not be included in the results.

Ben
Paula
Samuel

In the next examples I will be using a product class and two lists of products. Notice how productA and productB are declared and instantiated outside of the array initialisations.

public class Product
{
    public string Code { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
}
.
...
.
var productA = new Product { Code = "CD5", Name = "Paint", Category = "Decorating" };
var productB = new Product { Code = "FG9", Name = "Ladder", Category = "Access" };
Product[] listm = {new Product{Code="AM1",Name="Bucket", Category="Decorating"},
                    new Product{Code="AM3",Name="Brush", Category="Decorating"},
                    productA,
                    productB,
                    new Product{Code="MN4",Name="Table", Category="Furniture"}
                    };
Product[] listn = {new Product{Code="CV6",Name="Brush", Category="Decorating"},
                    productA,
                    productB,
                    new Product{Code="Am3",Name="Brush", Category="Decorating"},
                    };

Calling the Intersect method on lists of objects in this way will only return those objects that are the same instances.

Console.WriteLine("object intersect...");
Console.WriteLine("IEnuerable.Intersect<TSource>...");
var products = listm.Intersect(listn);
products.ToList().ForEach(p => Console.WriteLine(p.Code));

So by default, the results only include the two pre-instantiated objects because, as well as being in both lists, they are the same instance.

CD5
FG9

There are two more scenarios I have been considering:
1. Get a list of all product codes in both lists.
2. Get a list of all product objects where the product code is in both lists.

I initially solved the first scenario in the following way. Note how the Select() method is used to retrieve the product codes so the Intersect() method is comparing two lists of string rather than objects. also note the use of the StringComparer to ignore text case.

var codesLINQ = listm.Select(a => a.Code).Intersect(listn.Select(b => b.Code), StringComparer.OrdinalIgnoreCase);
codesLINQ.ToList().ForEach(p => Console.WriteLine(p));

As we are comparing two lists of strings, the output includes the AM3 product code because it exists in both lists even though it is not the same object instance and we are ignoring case.

AM3
CD5
FG9

The second scenario was solved using a Where() method to retrieve the objects from listm that match the product code in listn. Note again this is ignoring case.

var productsLINQ = listm.Where(a => listn.Any(b => string.Compare(b.Code, a.Code, true) == 0));
productsLINQ.ToList().ForEach(p => Console.WriteLine(p.Code));

The output to the console for this example will be the same as the previous example, the only difference is that the productsLINQ variable contains the product objects not just the product codes.

In these two scenarios LINQ has been used in some form but it is possible to get the desired results without LINQ. As these two scenarios above are so similar their solution is similar too, so I am going to focus on solving the second one without using LINQ. To solve this problem we could change the product class could implement the IEquatable generic interface or to implement the IEqualityComparer generic interface in a helper class. The advantage of the latter is that your product class better follows the Single Responsibility Principle in that it will only need to change if your product info changes, not if the comparison criteria changes. For additional information check the MSDN documentation here. Firstly the IEquatable implementation.

public class Product : IEquatable<Product>
{
    public string Code { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }

    public bool Equals(Product other)
    {
        //Check whether the compared object is null.
        if (Object.ReferenceEquals(other, null)) return false;

        //Check whether the compared object references the same data.
        if (Object.ReferenceEquals(this, other)) return true;

        //Check whether the products' properties are equal.
		return string.Compare(Code, other.Code, true) == 0;

		// alternative comparison code.
		//return Code.ToUpper().Equals(other.Code.ToUpper());
    }

    // If Equals() returns true for a pair of objects
    // then GetHashCode() must return the same value for these objects.

    public override int GetHashCode()
    {
        //Get hash code for the Code field.
        int hashProductCode = Code.ToUpper().GetHashCode();

        //Calculate the hash code for the product code.
        return hashProductCode;
    }
}

By implementing the IEquatable interface we can now control how the objects are compared. This implementation only compares the product code.
Calling the Intersect() method does not change from earlier.

var products = listm.Intersect(listn);
products.ToList().ForEach(p => Console.WriteLine(p.Code));

The products variable holds the product objects that match the comparison code. In the earlier example when the Intersect() method was called before the IEquatable interface was implemented we got two product in the result. Now with the interface implemented there are three product products in the match.

AM3
CD5
FG9

The next example (the better example IMHO) shows how to implement the IEqualityComparer interface in a ProductComparer class.
Notice how the Code and Name properties are being compared to identify a match.

public class Product
{
    public string Code { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
}

public class ProductComparer : IEqualityComparer<Product>
{
    public bool Equals(Product x, Product y)
    {
        //Check whether the objects are the same object.
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether the products' properties are equal.
        return x != null && y != null
            && x.Code.ToUpper().Equals(y.Code.ToUpper())
            && x.Name.ToUpper().Equals(y.Name.ToUpper());

		// Alternative comparison code
		/*return x != null && y != null
            && string.Compare(x.Code, y.Code, true) == 0
            && string.Compare(x.Name, y.Name, true) == 0;*/
    }

    public int GetHashCode(Product obj)
    {
        //Get hash code for the Name field if it is not null.
        int hashProductName = obj.Name == null ? 0 : obj.Name.ToUpper().GetHashCode();

        //Get hash code for the Code field.
        int hashProductCode = obj.Code.ToUpper().GetHashCode();

        //Calculate the hash code for the product.
        return hashProductName ^ hashProductCode;
    }
}

When calling the Intersect() method you pass an instance of the ProductComparer as a parameter.

var products = listm.Intersect(listn, new ProductComparer());
products.ToList().ForEach(p => Console.WriteLine(p.Code));

As well as single responsibility, another advantage to implementing the IEqualityComparer interface over the IEquatable interface is you could have multiple ProductComparer classes with different criteria e.g. one that compares the product code and one that compares the code and Name properties. You decide which one you pass to the Intersect() method.