101 LINQ Samples
Learn how to use LINQ in your applications with these code samples, covering the entire range of LINQ functionality and demonstrating LINQ with SQL, DataSets, and XML.
http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
Thursday Jan 19th
C# dictionary conversion for JSON
Today I needed to send a JSON friendly dictionary to the serialiser. LINQ came to the (writing it the short way) rescue.
Dictionary<T, T> dictionary = COM.Layer.GetTheDictionary(arg);
// convert
Dictionary<string, string> jsonDictionary = dictionary.ToDictionary(p => p.Key.ToString(), p => p.Value.ToString());
If you have complex <T> types, you could override ToString() add your own conversion to make sure everything is happy.
return jsonDictionary.ToJSON(); // ToJSON() string extension method
Thursday Jan 13th
Speed Test: Switch vs If-Else-If
Ridiculously geeky and can’t believe i’m posting this but still…
The switch statement vs else if ladder statements looped over a billion times.
http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx
Tuesday Nov 16th
Culture sensitive string.Contains()
This will provide an Extension Method to enable you to perform a safer contains search within a string.
For projects built on framework v2.0, make sure you include:
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class
| AttributeTargets.Method)]
public sealed class ExtensionAttribute : Attribute
{
public ExtensionAttribute() { }
}
}
Here’s the method:
public static bool Contains(this string searchWithin,
string value, StringComparison comparisonOptions)
{
if (value.Length == 0)
{
return true;
}
for (int i = 0; i < (searchWithin.Length - value.Length); i++)
{
if (searchWithin.Substring(i, value.Length).Equals(value, comparisonOptions))
{
return true;
}
}
return false;
}
something.Contains(somethingElse, StringComparison.CurrentCultureIgnoreCase);
See here http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/ebc4258ed2c4270f/c48b6c15df01e8bf?pli=1 for more information on the issue.
Wednesday Sep 22nd
Complete reusable UserControl library in .Net
Want a library of complete (including html rendering), re-usable user controls you can use across multiple projects? Read on…
I’ve recently worked on a project where the client wished to port an internal website (kiosk) to the web.
The project was built using lots of usercontrols (which was not ideal as this was the existing developers way to separate the business logic) and I wasn’t about to start copying these across the 2 projects.
I created an empty web site in the solution (not a web project) and copied across all the controls into the root. After deleting the designer files as these aren’t used in a web site project, I published the site to a directory with the following options:

This would publish the site and all necessary dll’s to the specified folder. Now, reference the web site assembly’s from the project you need them on and include a reference to them in the web.config:
<add tagPrefix=”uc” assembly=”Assembly.Name” namespace=”ASP”/>
Note: On inspection of the outputted dll in reflector an ASP namespace is created and this is the namespace you’ll need to use to get your user controls. Also, I haven’t found a way to keep the control name classes the same, you’ll see they are now something like ‘controlname_ascx’ and this is what you’ll be using in the code <uc:controlname_ascx runat=”server” />.
To make things more annoying, the resulting dll’s that are published are random and contain a hash of the directory in which the website lives - this is by design and used for conflicts. I use ILMerge to compile the assemblies into a single assembly with a pre-defined name so I don’t have to keep changing the references.
Monday Sep 20th