<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codelab Blog &#187; C#</title>
	<atom:link href="http://blog.codelab.co.nz/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.codelab.co.nz</link>
	<description>Technical Articles and News from Codelab Ltd</description>
	<lastBuildDate>Tue, 17 Jan 2012 13:10:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Some useful Extension Methods</title>
		<link>http://blog.codelab.co.nz/2011/09/06/some-useful-extension-methods/</link>
		<comments>http://blog.codelab.co.nz/2011/09/06/some-useful-extension-methods/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 10:45:59 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=358</guid>
		<description><![CDATA[Back to basics&#8230; A couple of useful Extension methods if you are checking if a string is a valid integer or decimal. public static bool IsInteger(this string value) { if (String.IsNullOrEmpty(value)) return false; Int32 tmpNo; return Int32.TryParse(value, out tmpNo); } public static bool IsDecimal(this string value) { if (String.IsNullOrEmpty(value)) return false; Decimal tmpNo; return Decimal.TryParse(value, [...]]]></description>
			<content:encoded><![CDATA[<p>Back to basics&#8230;</p>
<p>A couple of useful Extension methods if you are checking if a string is a valid integer or decimal.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public static bool IsInteger(this string value)<br />
{<br />
if (String.IsNullOrEmpty(value)) return false;<br />
Int32 tmpNo;<br />
return Int32.TryParse(value, out tmpNo);<br />
}<br />
<br />
<br />
public static bool IsDecimal(this string value)<br />
{<br />
if (String.IsNullOrEmpty(value)) return false;<br />
Decimal tmpNo;<br />
return Decimal.TryParse(value, out tmpNo);<br />
}</div></div>
<p>To implement the Extension Methods:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var myString = &quot;10.5&quot;;<br />
<br />
if (myString.IsDecimal())<br />
<br />
{<br />
<br />
//Do something<br />
<br />
}</div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2011/09/06/some-useful-extension-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OrderBy making null records come last Entity Framework and Linq</title>
		<link>http://blog.codelab.co.nz/2011/07/28/orderby-making-null-records-come-last-entity-framework-and-linq/</link>
		<comments>http://blog.codelab.co.nz/2011/07/28/orderby-making-null-records-come-last-entity-framework-and-linq/#comments</comments>
		<pubDate>Wed, 27 Jul 2011 11:45:02 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=352</guid>
		<description><![CDATA[Ever want to sort your result set but make sure the records with a null sorting column appear last? See the example below var results = (from x in EntityObjectContext.MyTable &#160; &#160; &#160; &#160; &#160; &#160; &#160; select x &#160; &#160; &#160; &#160; &#160; &#160; &#160; into grp &#160; &#160; &#160; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Ever want to sort your result set but make sure the records with a null sorting column appear last?<br />
See the example below</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var results = (from x in EntityObjectContext.MyTable<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select x<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; into grp<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select new<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mydata = grp,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myid = grp.id,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysortingentity = grp.mysortingentity<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;).OrderBy(x =&gt; sortingcolumn == null).ThenBy(x =&gt; x.mysortingentity.Name).ThenBy(x =&gt; x.myid);</div></div>
<p>Watch this space, this can easily be transformed into a Extension Method.  Check out this link <a href="http://tahirhassan.blogspot.com/2010/06/linq-to-sql-order-by-nulls-last.html">http://tahirhassan.blogspot.com/2010/06/linq-to-sql-order-by-nulls-last.html</a>, this Extension method works for LinqToSQL.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2011/07/28/orderby-making-null-records-come-last-entity-framework-and-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding StoreGeneratedPattern value in EdmProperty in T4 Template</title>
		<link>http://blog.codelab.co.nz/2011/05/11/finding-storegeneratedpattern-value-in-edmproperty-in-t4-template/</link>
		<comments>http://blog.codelab.co.nz/2011/05/11/finding-storegeneratedpattern-value-in-edmproperty-in-t4-template/#comments</comments>
		<pubDate>Wed, 11 May 2011 03:11:05 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T4 Templates]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=334</guid>
		<description><![CDATA[If you need to find out if a primary key in your conceptual model which is using the Identity value from the property StoreGeneratedPattern, you can use the following code: (NOTE: This is used within a T4 template) string inputFile = @&#34;..\EntitiesModel.edmx&#34;; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string annotationNamespace = &#34;http://schemas.microsoft.com/ado/2009/02/edm/annotation&#34;; foreach (EntityType entity in ItemCollection.GetItems&#60;EntityType&#62;().OrderBy(e [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to find out if a primary key in your conceptual model which is using the Identity value from the property StoreGeneratedPattern, you can use the following code: (NOTE: This is used within a T4 template)</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">string inputFile = @&quot;..\EntitiesModel.edmx&quot;;<br />
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);<br />
string annotationNamespace = &quot;http://schemas.microsoft.com/ado/2009/02/edm/annotation&quot;;<br />
<br />
foreach (EntityType entity in ItemCollection.GetItems&lt;EntityType&gt;().OrderBy(e =&gt; e.Name)) {<br />
&nbsp; &nbsp;foreach (EdmProperty edmProperty in entity.Properties.Where(p =&gt; p.TypeUsage.EdmType is PrimitiveType &amp;&amp; p.DeclaringType == entity)) {<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; MetadataProperty storeGeneratedPatternProperty = null;<br />
&nbsp; &nbsp; &nbsp; edmProperty.MetadataProperties.TryGetValue(annotationNamespace + &quot;:StoreGeneratedPattern&quot;, false, out storeGeneratedPatternProperty);<br />
<br />
&nbsp; &nbsp; &nbsp; if (storeGeneratedPatternProperty != null &amp;&amp; storeGeneratedPatternProperty.Value.ToString() == &quot;Identity&quot;) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//We found an Identity property<br />
&nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp;}<br />
}</div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2011/05/11/finding-storegeneratedpattern-value-in-edmproperty-in-t4-template/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Open XML &#8211; OLE Automation Date Issues</title>
		<link>http://blog.codelab.co.nz/2010/07/26/open-xml-ole-automation-date-issues/</link>
		<comments>http://blog.codelab.co.nz/2010/07/26/open-xml-ole-automation-date-issues/#comments</comments>
		<pubDate>Sun, 25 Jul 2010 12:16:20 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Excel]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=290</guid>
		<description><![CDATA[If you are exporting a date from C# to Excel using 2007, you probably will use the following: Math.Round(DateTime.Now.ToOADate(), 12).ToString() This exports the date as a OLE Automation date recognized by Excel 2007. How ever, in Excel 2010 this was causing issues, every time I exported to Excel 2010, it said that it has to [...]]]></description>
			<content:encoded><![CDATA[<p>If you are exporting a date from C# to Excel using 2007, you probably will use the following:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Math.Round(DateTime.Now.ToOADate(), 12).ToString()</div></div>
<p>This exports the date as a OLE Automation date recognized by Excel 2007.   How ever, in Excel 2010 this was causing issues, every time I exported to Excel 2010, it said that it has to repair my document and it never displayed the values correctly as dates.</p>
<p>Because I was using Open XML to generate the markup, Excel 2010 has been support for Open XML.   I got around the problem of using:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DateTime.Now.ToString(&quot;yyyy-MM-dd'T'HH:mm:ss.fffffffzzz&quot;);</div></div>
<p>This exports the date as a XML Date format.<br />
Hope this helps anyone who has the same problem!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2010/07/26/open-xml-ole-automation-date-issues/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fixing the EF Tracing and Caching Provider Wrapper Issue</title>
		<link>http://blog.codelab.co.nz/2010/07/09/fixing-ef-tracing-caching-wrapper-issue/</link>
		<comments>http://blog.codelab.co.nz/2010/07/09/fixing-ef-tracing-caching-wrapper-issue/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 04:20:59 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=268</guid>
		<description><![CDATA[If you have been using the Tracing and Caching Provider Wrappers for the ADO.NET Entity Framework 4.0, you might of come across this error message when creating POCO objects, adding them to a Data Context and commiting them to the database using the Caching Wrapper: [NotImplementedException: The method or operation is not implemented.] EFCachingProvider.EFCachingDataReaderCacheWriter.GetName(Int32 ordinal) [...]]]></description>
			<content:encoded><![CDATA[<p>If you have been using the <a href="http://code.msdn.microsoft.com/EFProviderWrappers">Tracing and Caching Provider Wrappers for the ADO.NET Entity Framework 4.0</a>, you might of come across this error message when creating POCO objects, adding them to a Data Context and commiting them to the database using the Caching Wrapper:<span style="font-family: Arial,Helvetica,Geneva,SunSans-Regular,sans-serif;"><br />
</span></p>
<pre>[NotImplementedException: The method or operation is not implemented.]
   EFCachingProvider.EFCachingDataReaderCacheWriter.GetName(Int32 ordinal) in EFCachingProvider\EFCachingDataReaderCacheWriter.cs:109
   System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues) +8118458
   System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) +267
</pre>
<p><span style="font-family: Arial,Helvetica,Geneva,SunSans-Regular,sans-serif;"> </span></p>
<pre>Line 107:        public override string GetName(int ordinal)
Line 108:        {
<span style="color: red;">Line 109:            throw new NotImplementedException();
</span>Line 110:        }
Line 111:
</pre>
<p>Basically, the author&#8217;s of the code have created a wrapper for the DBDataReader class and have not implement the override methods.   This can be fixed by calling the Wrapper DataReader object methods.  This is what I have modified in the EFCachignDataReaderCacheWriter.cs file:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">using System;<br />
using System.Data;<br />
using System.Data.Common;<br />
using EFCachingProvider.Caching;<br />
<br />
namespace EFCachingProvider<br />
{<br />
///<br />
/// Implementation of the &nbsp;which reads the results of another data reader<br />
/// and stores a copy in the cache.<br />
///<br />
internal class EFCachingDataReaderCacheWriter : EFCachingDataReaderBase<br />
{<br />
private DbQueryResults queryResults = new DbQueryResults();<br />
private DbDataReader wrappedReader;<br />
private int maxRows;<br />
private Action addToCache;<br />
<br />
///<br />
/// Initializes a new instance of the EFCachingDataReaderCacheWriter class.<br />
///<br />
///<br />
&lt;span&gt; &lt;/span&gt;The wrapped reader. &nbsp; &nbsp; &nbsp; &nbsp; ///<br />
&lt;span&gt; &lt;/span&gt;The maximum number of rows which can be cached. &nbsp; &nbsp; &nbsp; &nbsp; ///<br />
The delegate used to add the result to the cache when the reader finishes. &nbsp; &nbsp; &nbsp; &nbsp; public EFCachingDataReaderCacheWriter(DbDataReader wrappedReader, int maxRows, Action addToCache)<br />
{<br />
this.wrappedReader = wrappedReader;<br />
this.addToCache = addToCache;<br />
this.maxRows = maxRows;<br />
}<br />
<br />
///<br />
/// Gets a value that indicates whether this &nbsp;contains one or more rows.<br />
///<br />
///<br />
/// true if the &nbsp;contains one or more rows; otherwise false.<br />
///<br />
public override bool HasRows<br />
{<br />
get { return this.wrappedReader.HasRows; }<br />
}<br />
<br />
///<br />
/// Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.<br />
///<br />
///<br />
///<br />
/// The number of rows changed, inserted, or deleted. -1 for SELECT statements; 0 if no rows were affected or the statement failed.<br />
///<br />
public override int RecordsAffected<br />
{<br />
//get { throw new NotImplementedException(); }<br />
get { return this.wrappedReader.RecordsAffected; }<br />
}<br />
<br />
///<br />
/// Gets a value indicating whether the &nbsp;is closed.<br />
///<br />
///<br />
/// true if the &nbsp;is closed; otherwise false.<br />
///<br />
public override bool IsClosed<br />
{<br />
//get { throw new NotImplementedException(); }<br />
get { return this.wrappedReader.IsClosed; }<br />
}<br />
<br />
///<br />
/// Gets a value indicating the depth of nesting for the current row.<br />
///<br />
///<br />
///<br />
/// The depth of nesting for the current row.<br />
///<br />
public override int Depth<br />
{<br />
//get { throw new NotImplementedException(); }<br />
get { return this.wrappedReader.Depth; }<br />
}<br />
<br />
///<br />
/// Gets name of the data type of the specified column.<br />
///<br />
///<br />
The zero-based column ordinal. &nbsp; &nbsp; &nbsp; &nbsp; ///<br />
/// A string representing the name of the data type.<br />
///<br />
public override string GetDataTypeName(int ordinal)<br />
{<br />
//throw new NotImplementedException();<br />
return this.wrappedReader.GetDataTypeName(ordinal);<br />
}<br />
<br />
///<br />
/// Gets the data type of the specified column.<br />
///<br />
///<br />
The zero-based column ordinal. &nbsp; &nbsp; &nbsp; &nbsp; /// The data type of the specified column.<br />
public override Type GetFieldType(int ordinal)<br />
{<br />
return this.wrappedReader.GetFieldType(ordinal);<br />
}<br />
<br />
///<br />
/// Gets the name of the column, given the zero-based column ordinal.<br />
///<br />
///<br />
The zero-based column ordinal. &nbsp; &nbsp; &nbsp; &nbsp; /// The name of the specified column.<br />
public override string GetName(int ordinal)<br />
{<br />
return this.wrappedReader.GetName(ordinal);<br />
}<br />
<br />
///<br />
/// Gets the column ordinal given the name of the column.<br />
///<br />
///<br />
The name of the column. &nbsp; &nbsp; &nbsp; &nbsp; /// The zero-based column ordinal.<br />
///<br />
/// The name specified is not a valid column name.<br />
///<br />
public override int GetOrdinal(string name)<br />
{<br />
return this.wrappedReader.GetOrdinal(name);<br />
}<br />
<br />
///<br />
/// Returns a &nbsp;that describes the column metadata of the .<br />
///<br />
///<br />
/// A &nbsp;that describes the column metadata.<br />
///<br />
public override DataTable GetSchemaTable()<br />
{<br />
return this.wrappedReader.GetSchemaTable();<br />
}<br />
<br />
///<br />
/// Advances the reader to the next result when reading the results of a batch of statements.<br />
///<br />
///<br />
/// true if there are more result sets; otherwise false.<br />
///<br />
public override bool NextResult()<br />
{<br />
if (this.wrappedReader.NextResult())<br />
{<br />
this.queryResults = null;<br />
return true;<br />
}<br />
else<br />
{<br />
return false;<br />
}<br />
}<br />
<br />
///<br />
/// Closes the &nbsp;object.<br />
///<br />
public override void Close()<br />
{<br />
this.wrappedReader.Close();<br />
<br />
if (this.queryResults != null)<br />
{<br />
this.addToCache(this.queryResults);<br />
}<br />
}<br />
<br />
///<br />
/// Advances the reader to the next record in a result set.<br />
///<br />
///<br />
/// true if there are more rows; otherwise false.<br />
///<br />
public override bool Read()<br />
{<br />
if (this.wrappedReader.Read())<br />
{<br />
object[] values = new object[this.wrappedReader.FieldCount];<br />
<br />
this.wrappedReader.GetValues(values);<br />
SetValues(values);<br />
if (this.queryResults != null)<br />
{<br />
this.queryResults.Rows.Add(values);<br />
if (this.queryResults.Rows.Count &amp;gt; this.maxRows)<br />
{<br />
this.queryResults = null;<br />
}<br />
}<br />
<br />
return true;<br />
}<br />
else<br />
{<br />
return false;<br />
}<br />
}<br />
}<br />
}</div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2010/07/09/fixing-ef-tracing-caching-wrapper-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random number of items from a Generic List extension method</title>
		<link>http://blog.codelab.co.nz/2010/06/13/random-number-of-items-from-a-generic-list-extension-method/</link>
		<comments>http://blog.codelab.co.nz/2010/06/13/random-number-of-items-from-a-generic-list-extension-method/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 09:58:36 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=249</guid>
		<description><![CDATA[Here is a code snippet  for returning N number of items from a generic list using a extension method.  This requires .NET 3.5. public static class Extensions &#160; &#160; { &#160; &#160; &#160; &#160; /// &#60;summary&#62; &#160; &#160; &#160; &#160; /// method for returning N number of random items from a generic list &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a code snippet  for returning N number of items from a generic list using a extension method.  This requires .NET 3.5.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public static class Extensions<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// method for returning N number of random items from a generic list<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;typeparam name=&quot;T&quot;&gt;Item type&lt;/typeparam&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name=&quot;list&quot;&gt;Generic list we wish to retrieve from&lt;/param&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name=&quot;count&quot;&gt;number of items to return&lt;/param&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;returns&gt;&lt;/returns&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; public static IEnumerable&lt;T&gt; Randomize&lt;T&gt;(this List&lt;T&gt; list, int count)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List&lt;T&gt; randomList = new List&lt;T&gt;();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Random random = new Random();<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (list.Count &gt; 0)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //get the next random number between 0 and<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //the list count<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int idx = random.Next(0, list.Count);<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //get that index<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; randomList.Add(list[idx]);<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //remove that item so it cant be added again<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.RemoveAt(idx);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //return the specified number of items<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return randomList.Take(count);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;summary&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// method for returning 1 item from the generic list<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;/summary&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;typeparam name=&quot;T&quot;&gt;Item type&lt;/typeparam&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name=&quot;list&quot;&gt;Generic list we wish to retrieve from&lt;/param&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;param name=&quot;count&quot;&gt;number of items to return&lt;/param&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; /// &lt;returns&gt;&lt;/returns&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; public static T Randomize&lt;T&gt;(this List&lt;T&gt; list)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Randomize(list, 1).FirstOrDefault();<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }</div></div>
<p>used in code by calling the following: List&lt;mytype&gt; = myGenericList.Randomise(5) or mytype = myGenericList.Randomise();</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2010/06/13/random-number-of-items-from-a-generic-list-extension-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The custom tool ‘MSLinqToSQLGenerator’ failed. Unspecified error</title>
		<link>http://blog.codelab.co.nz/2010/01/18/the-custom-tool-%e2%80%98mslinqtosqlgenerator%e2%80%99-failed-unspecified-error/</link>
		<comments>http://blog.codelab.co.nz/2010/01/18/the-custom-tool-%e2%80%98mslinqtosqlgenerator%e2%80%99-failed-unspecified-error/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 08:29:15 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=172</guid>
		<description><![CDATA[I kept getting this error &#8220;The custom tool &#8216;MSLinqToSQLGenerator&#8217; failed.  Unspecified error.   For me, this was caused when I was using partial classes and had several using statements at the top of the file: using System.Data.Linq; using System.Data.Linq.Mapping; using System.Data; using System.Collections.Generic; using System.Reflection; using System.Linq; using System.Linq.Expressions; using System.ComponentModel; using System; {namespace} {code} To [...]]]></description>
			<content:encoded><![CDATA[<p>I kept getting this error &#8220;The custom tool &#8216;MSLinqToSQLGenerator&#8217; failed.  Unspecified error.   For me, this was caused when I was using partial classes and had several using statements at the top of the file:</p>
<p>using System.Data.Linq;<br />
using System.Data.Linq.Mapping;<br />
using System.Data;<br />
using System.Collections.Generic;<br />
using System.Reflection;<br />
using System.Linq;<br />
using System.Linq.Expressions;<br />
using System.ComponentModel;<br />
using System;</p>
<p>{namespace}</p>
<p>{code}</p>
<p>To get around this, I removed the using namespaces and ran the custom build tool, then readded the using statements which resolved my issue.   This issue apparently is in SP1 VS2008!</p>
<p><em><strong>References:</strong></em></p>
<p><a href="http://blog.unidev.com/index.php/2008/09/02/the-custom-tool-mslinqtosqlgenerator-failed-unspecified-error/">http://blog.unidev.com/index.php/2008/09/02/the-custom-tool-mslinqtosqlgenerator-failed-unspecified-error/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2010/01/18/the-custom-tool-%e2%80%98mslinqtosqlgenerator%e2%80%99-failed-unspecified-error/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Modify a Word 2007 Open XML document via C#</title>
		<link>http://blog.codelab.co.nz/2009/04/20/modify-a-word-2007-open-xml-document-via-c/</link>
		<comments>http://blog.codelab.co.nz/2009/04/20/modify-a-word-2007-open-xml-document-via-c/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 11:31:12 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=116</guid>
		<description><![CDATA[Interesting problem that I came across here&#8230;A client requested that their system was to have the ability to load in a Word 2007 Document, perform some text manipulation routines and save the document back to the server. Firstly, you can rename a docx document to docx.zip and you can extract all of the files in [...]]]></description>
			<content:encoded><![CDATA[<p>Interesting problem that I came across here&#8230;A client requested that their system was to have the ability to load in a Word 2007 Document, perform some text manipulation routines and save the document back to the server.</p>
<p>Firstly, you can rename a docx document to docx.zip and you can extract all of the files in the zip file that build the foundation of the Open XML document.   Not bad!</p>
<p>Here is the code that I used to open a Word 2007 document, do some manipulation and save the file.</p>
<p>using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(newPath, true))<br />
{</p>
<p>string docText;<br />
using (var sr = new StreamReader(wdDoc.MainDocumentPart.GetStream()))<br />
{<br />
docText = sr.ReadToEnd();<br />
}</p>
<p>docText = RegExpReplace(&#8220;{REPLACETEXTHERE}&#8221;, docText, &#8220;With this value&#8221;);<br />
using (var sw = new StreamWriter(wdDoc.MainDocumentPart.GetStream(FileMode.Create)))<br />
{<br />
sw.Write(docText);<br />
}</p>
<p>}</p>
<p>The WordprocessingDocument classes are part of the Open XML Format 1.0 SDK available from <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=AD0B72FB-4A1D-4C52-BDB5-7DD7E816D046&amp;displaylang=en">Microsoft</a>.</p>
<p>This beats having to do the old way of importing Office API Dlls via COM to load and write word documents!</p>
<p><em><strong>References:</strong></em></p>
<p><a href="http://msdn.microsoft.com/en-us/library/documentformat.openxml.packaging.wordprocessingdocument.aspx">http://msdn.microsoft.com/en-us/library/documentformat.openxml.packaging.wordprocessingdocument.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2009/04/20/modify-a-word-2007-open-xml-document-via-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Best Coding Practices &#8211; TIP 3</title>
		<link>http://blog.codelab.co.nz/2009/02/20/best-coding-practices-tip-3/</link>
		<comments>http://blog.codelab.co.nz/2009/02/20/best-coding-practices-tip-3/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 08:52:02 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding Practices]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=78</guid>
		<description><![CDATA[The use of partial classes can help improve coding for the following reasons: As you can split code into seperate physical files, its easier to seperate UI and Business Logic that may belong in a single class Produces clean and organised code No performance hit&#8230; The compiler groups all partial classes into one entity during [...]]]></description>
			<content:encoded><![CDATA[<p>The use of partial classes can help improve coding for the following reasons:</p>
<ul>
<li>As you can split code into 	seperate physical files, its easier to seperate UI and Business 	Logic that may belong in a single class</li>
<li>Produces clean and organised code</li>
<li>No performance hit&#8230; The compiler groups all partial classes into one entity during compilation</li>
</ul>
<p style="margin-bottom: 0cm;"><em><strong>Sample Code</strong></em></p>
<p style="margin-bottom: 0cm;">MyFile1.cs</p>
<p style="margin-bottom: 0cm;">public partial class MyPartialClass</p>
<p style="margin-bottom: 0cm;">{</p>
<p style="margin-bottom: 0cm;">int foo = 0;</p>
<p style="margin-bottom: 0cm;">}</p>
<p style="margin-bottom: 0cm;">MyFile2.cs</p>
<p style="margin-bottom: 0cm;">public partial class MyPartialClass</p>
<p style="margin-bottom: 0cm;">{</p>
<p style="margin-bottom: 0cm;">public String ShowMessage()</p>
<p style="margin-bottom: 0cm;">{</p>
<p style="margin-bottom: 0cm;">return foo.ToString();</p>
<p style="margin-bottom: 0cm;">}</p>
<p style="margin-bottom: 0cm;">}</p>
<p style="margin-bottom: 0cm;"><em><strong>REMEMBER:</strong></em> Keep partial classes in the same namespace to avoid confusion!</p>
<p style="margin-bottom: 0cm;">
<p style="margin-bottom: 0cm;">
<p style="margin-bottom: 0cm;">
<p style="margin-bottom: 0cm;">
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2009/02/20/best-coding-practices-tip-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using ContextMenuStrip and NotifyIcon classes with WPF</title>
		<link>http://blog.codelab.co.nz/2009/02/10/using-contextmenustrip-and-notifyicon-classes-with-wpf/</link>
		<comments>http://blog.codelab.co.nz/2009/02/10/using-contextmenustrip-and-notifyicon-classes-with-wpf/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 21:44:26 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=69</guid>
		<description><![CDATA[There are many ways to get a notification icon displaying in the system tray, this is how I managed to get it working using Windows Presentation Framework: public partial class ClassName : Window { private System.Windows.Forms.NotifyIcon m_notifyIcon; private System.Windows.Forms.ContextMenuStrip m_contextMenu; public ClassName() { InitializeComponent(); //Initalize the context menu strip m_contextMenu = new System.Windows.Forms.ContextMenuStrip(); System.Windows.Forms.ToolStripMenuItem mI1 [...]]]></description>
			<content:encoded><![CDATA[<p><!--[endif]--></p>
<p class="MsoNormal"><span lang="EN-NZ">There are many ways to get a notification icon displaying in the system tray, this is how I managed to get it working using Windows Presentation Framework:</span></p>
<p class="MsoNormal"><span lang="EN-NZ"> </span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: blue;">public</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"> <span style="color: blue;">partial</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ClassName</span> : <span style="color: #2b91af;">Window</span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;">{</span></p>
<p class="MsoNormal"><span lang="EN-NZ"> </span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: blue;"> private</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"> System.Windows.Forms.<span style="color: #2b91af;">NotifyIcon</span> m_notifyIcon;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: blue;"> private</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"> System.Windows.Forms.<span style="color: #2b91af;">ContextMenuStrip</span> m_contextMenu;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"> </span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: blue;">public</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"> <span style="color: #2b91af;">ClassName</span>()</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>InitializeComponent();</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span><span style="color: green;">//Initalize the context menu strip</span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>m_contextMenu = <span style="color: blue;">new</span> System.Windows.Forms.<span style="color: #2b91af;">ContextMenuStrip</span>();</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>System.Windows.Forms.<span style="color: #2b91af;">ToolStripMenuItem</span> mI1 = <span style="color: blue;">new</span> System.Windows.Forms.<span style="color: #2b91af;">ToolStripMenuItem</span>();</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>mI1.Text = <span style="color: #a31515;">&#8220;Menu One&#8221;</span>;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>mI1.Click += <span style="color: blue;">new</span> <span style="color: #2b91af;">EventHandler</span>(Click_Handler);<span style="color: green;"> //Add Click Handler</span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>m_contextMenu.Items.Add(mI1);</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>System.Windows.Forms.<span style="color: #2b91af;">ToolStripMenuItem</span> mI2 = <span style="color: blue;">new</span> System.Windows.Forms.<span style="color: #2b91af;">ToolStripMenuItem</span>();</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>mI2.Text = <span style="color: #a31515;">&#8220;Menu Two&#8221;</span>;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>mI2.Click += <span style="color: blue;">new</span> <span style="color: #2b91af;">EventHandler</span>(Click_Handler);<span style="color: green;"> //Add Click Handler</span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>m_contextMenu.Items.Add(mI2);</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span><span> </span><span style="color: green;">//Initalize Notify Icon</span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>m_notifyIcon = <span style="color: blue;">new</span> System.Windows.Forms.<span style="color: #2b91af;">NotifyIcon</span>();</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span><span> </span>m_notifyIcon.Text = <span style="color: #a31515;">&#8220;Application Title&#8221;</span>;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>m_notifyIcon.Icon = <span style="color: blue;">new</span> System.Drawing.<span style="color: #2b91af;">Icon</span>(<span style="color: #a31515;">&#8220;Icon.ico&#8221;</span>);</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>m_notifyIcon.ContextMenuStrip = m_contextMenu; <span style="color: green;">//Associate the contextmenustrip with notify icon</span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>m_notifyIcon.Visible = <span style="color: blue;">true</span>;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;">}</span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2009/02/10/using-contextmenustrip-and-notifyicon-classes-with-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

