Fizz-Buzz in LINQ

This just occurred to me. It is somewhat pointless, but I thought it was interesting:

static void Main(string[] args)
{
    var result = from say in Enumerable.Range(1, 100)
                 select (say % 15 == 0) ? "BuzzFizz" :
                    (say % 5 == 0) ? "Buzz" :
                        (say % 3 == 0) ? "Fizz" : say.ToString();
    foreach (string say in result)
        Console.WriteLine(say);
}
Technorati Tags: ,,

Getting values out of XML in .NET 3.5 (LINQ to XML series part 2)

In my last post I gave a brief introduction to some of the new XML classes available in .NET 3.5. In this post I’ll continue that introduction by explaining how to get information out of the XML.

First off, lets assume we have some XML that looks like this:

XElement root = new XElement("root",
    new XAttribute("Attribute", "TheValue"),
    new XElement("FirstChild"),
    new XElement("SecondChild", new XElement("Grandchild", "The content of the grandchild")));

or, if you prefer in XML format, like this:

<root Attribute=”TheValue”>

<FirstChild />

<SecondChild>

<Grandchild>The content of the grandchild</Grandchild>

</SecondChild>

</root>

There are a number of ways to get the content of the grandchild element. For example:

Console.WriteLine(root.Element("SecondChild").Element("Grandchild").Value);

Value returns a string which contains the content of the element specified. In the above case it will output:

The content of the grandchild

However, you need to watch out for when there are child elements of the thing you want the value of as their content is included when you get the value. For example, if the above XML is extended so that it looks like this:

<root Attribute=”TheValue”>

<FirstChild />

<SecondChild>

<Grandchild>The content of the grandchild

<Great-grandchild>GGC content</Great-grandchild>

<Grandchild>

</SecondChild>

</root>

And the above line of C# is executed again, the result is now:

The content of the grandchildGGC content

As you can see the content of the element you want plus its child elements are now returned. This may not necessarily be what you want.

There is a second way to get the content from an element. That is to use a casting operator. You can cast the element to a number of types. In this case to a string. for example:

Console.WriteLine((string)root.Element("SecondChild").Element("Grandchild"));

The result is the same as calling Value on the element.

Be careful here, because casting an element to a string will not have the same result as calling ToString() on an element. You can see that if you simply pass the element itself to writeline (which will then call ToString() for you). For example:

Console.WriteLine(root.Element("SecondChild").Element("Grandchild"));

The result is:

<Grandchild>The content of the grandchild

<Great-grandchild>GGC content</Great-grandchild>

</Grandchild>

The process is similar if you are dealing with attribute. Using the above XML as an example, an attribute value can be retrieved like this:

Console.WriteLine(root.Attribute("Attribute").Value);

Or, using the cast operator to a string like this:

Console.WriteLine((string)root.Attribute("Attribute"));

Both of the above pieces of code output the same thing: TheValue

If you were to call WriteLine on an XAttribute object you’ll see that the ToString() method returns something slightly different. It returns this: Attribute=”TheValue”

Lets say, for instance, that the Attribute had a value of 123.456, which is a valid number representation. I mentioned earlier about casting operators on XElement and XAttribute. Well, you can cast this to a double if you prefer to get the value in that type. There is no tedious converting in your own code as the framework can handle that for you. For example:

(double)root.Attribute("Attribute")

That’s it for this post. There will be more on XML and LINQ soon.

Data Protection Muppets

I’ve mentioned this topic on my blog before with regard to the Royal Bank of Scotland and Intelligent Finance but this time it was related to an insurance claim. The insurance company put me in contact with a company that would do the repairs and all they had to do was arrange a time and date. However, it wasn’t that simple.

Initially things seemed to be going well until the company in question phoned me to change the date because they wouldn’t have the materials in time. However, first they wanted to go through security screening.

Now, the conversation to this point had gone something like this:

Me: Hello
Them: Hello, is that Colin Mackay [pronounced kae – I HATE that!]
Me: Mackay [pronounced correctly – its a diphthong, a sliding or gliding vowel that goes from ‘ah’ to ‘ee’] Yes.
Them: This is Martindales. We just need to ask you some security questions before we proceed.
Me: How do I know you are who you say you are?
Them: We are Martindales, your insurance company has appointed us…

The conversation went from bad to worse as I tried to explain that what they are doing is socially conditioning people to hand out sensitive information and was then told that they “had to” ask these questions because of the data protection act. The act makes no such requirement. What they have to do is ensure that they are speaking to the correct person so they don’t divulge potentially sensitive information to the wrong person. However, the way they are going about it, while technically in line with the act, is most certainly not within the spirit of the act.

What made it worst was that when I was asked how they could continue the conversation and I gave the solution they had to ask me no fewer than 3 times how they were going to continue the conversation even although I had given them a solution. After that incident they decided they must not have like my simple solution and refused to communicate with me at all for a while.

My solution, incidentally, was this. They would phone me and indicate that they need to speak to me. I would then get the phone number from existing documentation (i.e. a trusted source) and phone their switchboard and ask to be put through to the person that needed to talk to me. They can then go through the security questions as I will then know I am talking to the correct party. When they phone me I have no way of knowing who I am talking to. They could be making it up. If they give me a phone number to use I won’t use it. I will only use trusted sources like documentation from my insurance company, or from the booklet that the insurance assessor left me.

Anyway, Martindales eventually decided that they did need to communicate with me about yet another change in date and sent me a letter. Pity it didn’t arrive until two days after the guy was supposed to show up. In fact he did almost arrive, and I only knew about it because they phoned me just to say that he was running a little late. Muppets!

 

Cool switch snippet

I was watching one of the MSDN Screencasts today and Mike Taulty put in a switch statement that pre-populated itself with valid values for each of the case statements within the switch. I hadn’t seen this before so I investigated further (in other words, I emailed Mike and asked him what he did). It turns out this is a feature that has been in since Visual Studio 2005 and I’d only just noticed.

Essentially, if you are switching on an enumerator the snippet will expand with all the case statements created for you as you can see by the animation below. To access this, follow these steps

  • Type “switch”, the intellisense will show the word “switch” with the torn document icon, indicating it is a snippet.
  • Press the tab key twice to expand the snippet, this will also highlight the text “switch_on”.
  • Change the “switch_on” text to the name of the variable on which you want to switch.
  • Press return twice, this will further expand the switch statement filling in all the cases from the enumerator.

For an example, see the animation below:

switch snippet

Technorati Tags: ,,

 

 

Donate to Sense Scotland

A friend of mine, Martin Bell, is going to do a crazy thing for charity to help raise money for Sense Scotland right after Developer Day Scotland. Sense Scotland is a charity that works with children and adults who have communication support needs because of deaf-blindness, sensory impairment, learning and physical disabilities. It is an excellent cause and the money will be put to very good use.

If you want to sponsor him then you can go to his sponsorship page and donate what ever you feel is appropriate. If you are a UK tax payer then you can also “gift aid” it where the Government will gift the tax to the charity too.

Aggressive Spam Filters

Today I discovered quite by accident that I have 9 genuine emails sitting in gmail’s spam trap that were sent to me over the last 24 hours. The genuine emails were from Microsoft, Quest, Redgate and Scottish Developers! I hit the “not spam” button for each of them so hopefully gmail learns and I’ll get them in future, but for the next few days I’ll be watching my spam folder for potential genuine messages disappearing.

 

Technorati Tags: ,

Scottish Developers Event Newsletter

As some of you may have been aware, Microsoft recently formally launched Visual Studio 2008, Windows Server 2008 and SQL Server 2008 in their Heroes Happen Here launch event in Birmingham. Well, if you didn’t make it to Birmingham don’t worry about it because we are doing our own community launch in Glasgow. I’m currently getting all the updates from DHL about the progress of the swag (prizes) as it makes its way from Redmond. So there will be lots of giveaways on the night, so if you want a chance of winning some free software then let us know you’re coming. It is the usual venue at Glasgow Caledonian University, but we’re starting a little bit earlier than normal and we’re on a Thursday.

Also, if you have not registered as a delegate for Developer Day Scotland, now is your chance. The delegate registration is now open. There are three tracks in total for this day long event with some excellent presentations lined up.

 

Visual Studio 2008 Community Launch

Thursday April 3rd in Glasgow starting at 18:30

Speaker: Mike Ormond

In this session we will explore what’s new for web developers in Visual Studio 2008. Demos will include the new HTML designer, CSS enhancements, improvements when working with scripting and the three new ASP.NET 3.5 data controls. We may even throw a bit of AJAX in there, just for fun!

Scottish SQL Server User Group

Wednesday 23rd April in Edinburgh starting at 18:30

Speakers: Alistair Board & Satya SK Jayanty

Alistair will be talking about the 6 W’s of SQLDiag. Satya will be talking about SQL Server 2008 – Declarative Management Framework Concepts & Database Problem Solutions with Policy Management.

Developer Day Scotland

Saturday 10th May in Glasgow starting at 09:00

This is a three track day long event. 2 Developer! Developer! Developer! tracks and 1 SQLBits track. Delegate registration is open.

Web Client Software Factory

You may remember me reviewing Gary Short‘s talk on the Web Client Software Factory a while back. I also said I would ask him to present it to Scottish Developers in Glasgow, which hasn’t happened yet (we got him to talk about his favourite design patterns). However, he was voted in to do that talk at Developer Day Scotland which is being held in Glasgow on 10th May 2008. As you can see from the agenda he’ll be on in the morning in room 15/16.

The delegate registration is now open if you want to see the session along with other fantastic sessions.

 

In related news, Gary is now a Technical Evangelist for Developer Express. Congratulations Gary!

 

 

Developer! Developer! Developer! is coming to Scotland