Linq causes the Setup project to fail when building

All of a sudden, my setup project stopped building. It was very strange - when I tried to build it, it would say that the build failed, but it gave no errors. Nothing anywhere.

Thankfully, I’m not the first person to have run across this. It turns out that adding Linq to one of the projects in my solution caused this failure - it appears to be a bug in Visual Studio.

The solution appears at Microsoft’s Connect site (be sure to look in the ‘Workarounds’ tab) - https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=317870

Here is what Daren wrote:

1. Close VS 2008.
2. Open the project file containing the LINQ To SQL item in Notepad.
3. Remove the following lines:

1
2
3
<ItemGroup>
<Service Include=”{3259AA49-8AA1-44D3-9025-A0B520596A8C}” />
</ItemGroup>

The Setup Project will now build successfully. However, if you double-click the DBML file to open the designer in VS 2008 the Setup Project will stop building again. The above lines do not get re-added to the project file but the Setup Project will stop building anyway. Just restart VS 2008 and it will work again – until you open the DBML designer again. Once the Setup Project fails due to this problem it will never build successfully until after you restart VS 2008.

OOO, C#, and C/AL

I’ve been working with building some web services built on the Microsoft Dynamics Nav 2009 web services platform. Microsoft is doing something fairly slick here - you write code in the C/AL language, save it as a Nav Object, expose it as a web service, and the web services layer automatically translates the C/AL to C# and runs it in .Net. This allows you to debug the code using visual studio, among other things.

But it’s not perfect. Sometimes it makes mistakes.

I had this line of code in C/AL

1
IF AVSetup.”Sub Award Dimension No.” - 1 IN [1..8] THEN BEGIN

Which auto-translated to this in C#:

if(aVSetup.Target.GetFieldValueSafe(90, NavType.Integer)
  .ToInt32()-(1 >= 1 && 1

Oops. The order of operations got mixed up. The C/AL code subtracts one, then sees if the value is between 1 and 8. The C# sees if 1 is between 1 and 8 (getting a boolean value) and then tries to subtract the boolean value from the number. C# then gives this error:

Operator ‘-‘ cannot be applied to operands of type ‘int’ and ‘bool’

Thankfully, the solution for this is simple. All you have to do is use extra parentheses to explicitly set the Order of Operations.

1
IF ((AVSetup.”Sub Award Dimension No.” - 1) IN [1..8]) THEN BEGIN