Saturday, September 18, 2010

Enumerable Select Magic in C#

I'd always wanted to toy with the "Select" extension method that lives in the System.Linq namespace, and today I found a great opportunity to use it.

I had to calculate the number of working days between two dates (e.g. no weekends), and to do this I used a mix of the Range method on the Enumerable class and the select method, plus a list of the weekdays. The result is below.

public int WorkingDays(DateTime startDate, DateTime endDate)
{
int totalDays = endDate.Subtract(startDate).Days + 1;

List<DayOfWeek> businessDays = new List<DayOfWeek>()
{ System.DayOfWeek.Monday, System.DayOfWeek.Tuesday, System.DayOfWeek.Wednesday,
System.DayOfWeek.Thursday, System.DayOfWeek.Friday };

return Enumerable.Range(0, totalDays)
.Select( d => startDate.AddDays(d))
.Where( wd => businessDays.Contains(wd.DayOfWeek)).Count();

}

 

Anyway, I think it's a great basic example of how to use the select method, so I thought I'd share it.

Saturday, June 19, 2010

Visual Studio 2010, Web.Config Transformations and the Broken Bits

For those of you who are deploying web applications using the new Visual Studio 2010 features in the RTM version, keep these issues in mind when you are using the new web.config transformation features:


xdt:transform="Replace" attributes don't work properly

If you are are using application settings with a .net Project, you'll find that the XML will actually stored the value in an element rather than as a attribute. When you try to apply the "Replace" tranform on this, you'll find that the result after publishing changes the value from this

<value>mystringvalue</value>

to this

<value>mystringvalue
</value>

Unfortunately, this actually adds the spaces to the end of your value, and the only way round it at the moment is to actually modify your code with a string.Trim() function to ignore the additional spaces. This probably also occurs anywhere you have an element representing a value rather than an attribute representing a value in your web.config.

For more information, see here and here for more information on the issue.

xdt:transform="XSLT" does not exist and is not valid although documented!

When you have a look at the documentation around the web.config transform, you'll find that there's this excellent feature where you can have a standard XSLT transform. Well, it looks like it was accidentally omitted from the final release of Visual Studio 2010, because when you go to use it, the syntax is not recognized at all, in fact it gives you this message:

"Could not resolve 'XSLT' as a type of Transform".

You can find more information about the issue here.

Cheers

Matt.

Saturday, June 12, 2010

CRM 4.0 QUERY T4 Template

Hi Everyone,

I've finally got around to publishing some work I've done recently which leverages LINQ to SQL, T4 Templates and the CRM 4.0 API to create a data access layer to CRM.

Originally I started write a Query provider for LINQ, but found it to be an extreme amount of work - and decided instead to return to old faithful - LINQ to SQL - which I have a good grounding in.

Recently, Microsoft have added a LINQ to CRM query provider to the SDK (Version 4.0.12) so you should check that out. If it doesn't suit you, here's an alternative.

Features
  • Uses LINQ to SQL for Querying, meaning that you have the complete power of LINQ to SQL at your disposal
  • Database Queries do not go through CRM API, so they are potentially faster
  • Uses the CRM 4.0 API for CUD (Create, Update, Delete) operations - so data integrity is maintained.
  • Can generate only selected entities, rather than the entire CRM Entity list.
  • Works with WCF RIA services and any other technology that takes advantage of LINQ to SQL
  • You don't need to Leave the Visual Studio Environment, T4 generation is done within Visual Studio.

Anyway, if you have the time or the need, check it out here.

Cheers

Matt



Friday, May 7, 2010

CRM 4.0.12 SDK Release

Microsoft have just released the Microsoft CRM 4.0.12 SDK, which has a number of new developer features.

I'm still going through them, but I'm liking what I see.

One that stood out to me is that they now have a CRM 4.0 LINQ provider included! Yay!

To download the SDK and check it out, find it here:

http://www.microsoft.com/downloads/details.aspx?FamilyID=82E632A7-FAF9-41E0-8EC1-A2662AAE9DFB&displaylang=en

Cheers

Matt

Monday, April 19, 2010

LINQ to SQL Entity Base Version 1.2 Final

Hi everyone,

I've published the latest version of the Entity Base (Version 1.2) on Codeplex.

There were no source code changes since Version 1.2 Beta, although if you were previously on 1.1 there are a few bug fixes that you may be interested in.

You can find the release here:

http://linq2sqleb.codeplex.com/releases/view/43763

Cheers

Matt.

Saturday, March 13, 2010

Windows 7 HomeGroup Issue - possible solution

I had been trying to setup my network at home with a home group, so that all documents, printers, movies, etc... where available easily from any PC in the house.

Although I could join the homegroup with no issue, none of the computers could see the shares on the other, nor the default profile folders (documents, pictures etc..) and when I tried to view the full network map (found under the "Network and Sharing Center"), it was showing a strange network topology that didn't look anything like what I had, and didn't show the other machines properly.

Everything was configured correctly - but it just didn't work. How Frustrating.

After fiddling a good deal and looking up a few solutions in various forums, I almost gave up when I though i might check the configuration of the computer name, and found the setting below in the screenshot which I don't recall seeing before, but there it was under the System Settings -->Change Settings-->Network ID.

Changing the value from "This computer is part of a busines network etc..." to "This is a home computer; it's not part etc.." fixed the issue.

The Network map was now correct (infact I was impressed, it was spot on - even with multiple routers!), and so I repeated these steps on each computer in the network which also fixed their maps as well.

I then removed all computers from the home group, create a new home group and joined all the PC's again to it. Once I did this, the home group was up and running. I have no idea why this works, and ever after re-booting it's back to the "business network" option, but all I can say is it solved my issue and now I can enjoy the fruits of Windows 7.