Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

Group By with Count in Linq September 29, 2009

Filed under: C#,Linq — Rupert Bates @ 8:26 pm

To group a sequence getting a count for each element of the grouped sequence using Linq:

            var wordList = new List<String> { "test", "one", "test", "two" };
            var grouped = wordList
                .GroupBy(i => i) //Group the words
                .Select(i => new { Word = i.Key, Count = i.Count() }); //get a count for each

Which results in this sequence:

watch

Advertisements
 

Open your hosts file in notepad from Powershell

Filed under: Uncategorized — Rupert Bates @ 12:36 pm
Tags:

notepad (gci -Include hosts -Path c:\windows\system32 -Recurse)

 

When two problems appear at the same time no matter how seemingly unrelated, they probably are September 18, 2009

Filed under: Uncategorized — Rupert Bates @ 10:02 am

I’ve seen two examples of this in the last few days.

Firstly we have an Asp.Net website which has recently moved to a new datacentre and seemed to be working well, but then the other day it fell over big time and the event log started filling up with errors about deadlocks in the aspnet_isapi.dll and faults in the W3SVC service. We googled away and just came up with some thing about thread contention and upping the maxWorkerThreads parameter in the machine.config which I didn’t want to do without understanding why this had suddenly become and issue.

At the same time somebody noticed that printing no longer worked in non Internet Explorer browsers, but we ignored that to focus on the more serious stability issue. It was only when I gave up on the first error and looked into the printing bug that I worked out what was going on. The printing page for Non IE browsers tried to make an http request to another page on the site to load the image it was trying to print. Stricter firewall rules in our new datacentre meant that this http request was being blocked meaning that the thread that was trying to make this request was also blocked until the request timed out, and this was what caused both the printing problem and the threading deadlock.

The second example of this maxim is a bit less high tech; my washing machine started leaking water through the door and we noticed the seal was a bit torn. At the same time it kept stopping with an error message (yup, even washing machines have them now) about the filter being blocked (which it wasn’t). It turns out that the loss of pressure from the leaking water affected its ability to pump out water, so it thought that the filter was blocked (that may not be 100% accurate, but when it comes to washing machines I’m not technical).

 

Display C# code in a WordPress Blog September 16, 2009

Filed under: Uncategorized — Rupert Bates @ 4:24 pm

A cool way to get WordPress to format and sytax highlight your C# code:

[sourcecode language=’csharp’]
string MyCode = “My Code”;
[/sourcecode]

Which appears in the post as:

string MyCode = "My Code";
 

Auto Initialize a Dictionary in C#

Filed under: Uncategorized — Rupert Bates @ 4:05 pm

I couldn’t find an example of using the new auto initialization syntax in C# 3.0 with a Dictionary so here is one:

var dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } };
 

Connect to IIS 6 over WMI with WMI CIM studio September 10, 2009

Filed under: Systems Administration — Rupert Bates @ 4:08 pm

Connect to:

\\[ServerName]\root\MicrosoftIISv2

In the connection dialog:

If you are logged in as a user who has admin rights on the server then check ‘Login as current user’, otherwise uncheck it and enter the credentials of a user who does.

Click ‘Options>>’ button:

Set Impersonation level to ‘Impersonate’

Set Authentication level to ‘Packet Privacy’

Check ‘Enable all privileges’

Click ‘OK’ and hey presto!

 

A Total Newbies guide to a few Linux commands September 2, 2009

Filed under: Linux — Rupert Bates @ 12:36 pm

This is just a reminder for me really, as I struggle to debug one of our LAMP sites after our Sys Admin left…

cd [directoryname] – Change directory

cd .. – Go up one directory

cd ../.. – Go up 2 directories

ls – List directory contents

cat [filename] – Show the contents of a text file

rm [filename] – Delete a file

rmdir [directory] – Delete a directory

rmdir -r [directory] – Delete a directory and all it’s contents

sh [filename] – Execute a shell script file

Tab – Complete a filename

Ctrl + c – Cancel a running command

crontab -l – List all the current user’s cron jobs

ps -A – List all running processes

ps -A | grep java – list all running java processes

find -name *blah*.zip – Find all zip files with blah in their name, more examples

grep “[string literal]” *.log -C 4 – Search for a string literal in all .log files and return 4 lines on either side of the matching line, more examples

tail [filename] -follow – Show the last 10 lines of a file and watch it for changes (Ctrl + c to cancel)