<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The Code Slinger</title>
	<atom:link href="http://thecodeslinger.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://thecodeslinger.wordpress.com</link>
	<description>Slingin Code Since 1998</description>
	<lastBuildDate>Mon, 05 Sep 2011 02:22:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='thecodeslinger.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>The Code Slinger</title>
		<link>http://thecodeslinger.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://thecodeslinger.wordpress.com/osd.xml" title="The Code Slinger" />
	<atom:link rel='hub' href='http://thecodeslinger.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Auto Generate History Table and Corresponding Trigger (SQL)</title>
		<link>http://thecodeslinger.wordpress.com/2011/02/11/auto-generate-history-table-and-corresponding-trigger-sql/</link>
		<comments>http://thecodeslinger.wordpress.com/2011/02/11/auto-generate-history-table-and-corresponding-trigger-sql/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 18:54:23 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[History Tables]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Trigger]]></category>

		<guid isPermaLink="false">https://thecodeslinger.wordpress.com/2011/02/11/auto-generate-history-table-and-corresponding-trigger-sql/</guid>
		<description><![CDATA[Extending the excellent work of this post by Bryan Massey, I’ve updated the script to replace the deprecated SysProperties table in lieu of the sys.extended_properties so that this procedure will operate in SQL 2008.  Enjoy! /************************************************************************************************************ Created By:  Bryan Massey Created On:  3/11/2007 Modified By: Peter Samwel Modified On: 2/11/2011 &#8212; Updated SysProperties table refs [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=69&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Extending the excellent work of <a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=84331" target="_blank">this post by Bryan Massey</a>, I’ve updated the script to replace the deprecated SysProperties table in lieu of the sys.extended_properties so that this procedure will operate in SQL 2008.  Enjoy!</p>
<p>/************************************************************************************************************<br />
Created By:  Bryan Massey<br />
Created On:  3/11/2007<br />
Modified By: Peter Samwel<br />
Modified On: 2/11/2011 &#8212; Updated SysProperties table refs (deprecated) to use the new (2008) extended_properties view.<br />
Comments:  Stored proc performs the following actions:<br />
1) Queries system tables to retrieve table schema for @TableName parameter<br />
2) Creates a History table (&#8220;History_&#8221; + @TableName) to mimic the original table, plus include<br />
additional history columns.<br />
3) If @CreateTrigger = &#8216;Y&#8217; then it creates an Update/Delete trigger on the @TableName table,<br />
which is used to populate the History table.<br />
******************************************* MODIFICATIONS **************************************************<br />
MM/DD/YYYY &#8211; Modified By &#8211; Description of Changes<br />
************************************************************************************************************/<br />
CREATE PROCEDURE [dbo].[History_Bat_AutoGenerateHistoryTableAndTrigger]<br />
@TableName VARCHAR(200),<br />
@CreateTrigger CHAR(1) = &#8216;Y&#8217; &#8212; optional parameter; defaults to &#8220;Y&#8221;<br />
AS</p>
<p>DECLARE @SQLTable VARCHAR(8000), @SQLTrigger VARCHAR(8000), @FieldList VARCHAR(6000), @FirstField VARCHAR(200)<br />
DECLARE @TAB CHAR(1), @CRLF CHAR(1), @SQL VARCHAR(1000), @Date VARCHAR(12)</p>
<p>SET @TAB = CHAR(9)<br />
SET @CRLF = CHAR(13) + CHAR(10)<br />
SET @Date = CONVERT(VARCHAR(12), GETDATE(), 101)<br />
SET @FieldList = &#8221;<br />
SET @SQLTable = &#8221;</p>
<p>DECLARE @TableDescr VARCHAR(500), @FieldName VARCHAR(100), @DataType VARCHAR(50)<br />
DECLARE @FieldLength VARCHAR(10), @Precision VARCHAR(10), @Scale VARCHAR(10),  @FieldDescr VARCHAR(500), @AllowNulls VARCHAR(1)</p>
<p>DECLARE CurHistoryTable CURSOR FOR</p>
<p>&#8211; query system tables to get table schema<br />
SELECT CONVERT(VARCHAR(500), SP2.value) AS TableDescription,<br />
CONVERT(VARCHAR(100), SC.Name) AS FieldName, CONVERT(VARCHAR(50), ST.Name) AS DataType,<br />
CONVERT(VARCHAR(10),SC.length) AS FieldLength, CONVERT(VARCHAR(10), SC.XPrec) AS FieldPrecision,<br />
CONVERT(VARCHAR(10), SC.XScale) AS FieldScale,<br />
CASE SC.IsNullable WHEN 1 THEN &#8216;Y&#8217; ELSE &#8216;N&#8217; END AS AllowNulls<br />
FROM SysObjects SO<br />
INNER JOIN SysColumns SC ON SO.ID = SC.ID<br />
INNER JOIN SysTypes ST ON SC.xtype = ST.xtype<br />
LEFT OUTER JOIN sys.extended_properties SP ON SC.ID = SP.major_id AND SC.ColID = SP.minor_id<br />
LEFT OUTER JOIN sys.extended_properties SP2 ON SC.ID = SP2.major_id AND SP2.minor_id = 0<br />
WHERE SO.xtype = &#8216;u&#8217; AND SO.Name = @TableName<br />
ORDER BY SO.[name], SC.ColOrder</p>
<p>OPEN CurHistoryTable</p>
<p>FETCH NEXT FROM CurHistoryTable INTO @TableDescr, @FieldName, @DataType,<br />
@FieldLength, @Precision, @Scale, @AllowNulls</p>
<p>WHILE @@FETCH_STATUS = 0<br />
BEGIN</p>
<p>&#8211; create list of table columns<br />
IF LEN(@FieldList) = 0<br />
BEGIN<br />
SET @FieldList = @FieldName<br />
SET @FirstField = @FieldName<br />
END<br />
ELSE<br />
BEGIN<br />
SET @FieldList = @FieldList + &#8216;, &#8216; + @FieldName<br />
END</p>
<p>IF LEN(@SQLTable) = 0<br />
BEGIN<br />
SET @SQLTable = &#8216;CREATE TABLE [DBO].[History_' + @TableName + '] (&#8216; + @CRLF<br />
SET @SQLTable = @SQLTable + @TAB + &#8216;[History' + @FieldName + '] [INT] IDENTITY(1,1) NOT NULL,&#8217; + @CRLF<br />
END</p>
<p>SET @SQLTable = @SQLTable + @TAB + &#8216;[' + @FieldName + '] &#8216; + &#8216;[' + @DataType + ']&#8216;<br />
IF UPPER(@DataType) IN (&#8216;CHAR&#8217;, &#8216;VARCHAR&#8217;, &#8216;NCHAR&#8217;, &#8216;NVARCHAR&#8217;, &#8216;BINARY&#8217;)<br />
BEGIN<br />
SET @SQLTable = @SQLTable + &#8216;(&#8216; + @FieldLength + &#8216;)&#8217;<br />
END<br />
ELSE IF UPPER(@DataType) IN (&#8216;DECIMAL&#8217;, &#8216;NUMERIC&#8217;)<br />
BEGIN<br />
SET @SQLTable = @SQLTable + &#8216;(&#8216; + @Precision + &#8216;, &#8216; + @Scale + &#8216;)&#8217;<br />
END</p>
<p>IF @AllowNulls = &#8216;Y&#8217;<br />
BEGIN<br />
SET @SQLTable = @SQLTable + &#8216; NULL&#8217;<br />
END<br />
ELSE<br />
BEGIN<br />
SET @SQLTable = @SQLTable + &#8216; NOT NULL&#8217;<br />
END</p>
<p>SET @SQLTable = @SQLTable + &#8216;,&#8217; + @CRLF</p>
<p>FETCH NEXT FROM CurHistoryTable INTO @TableDescr, @FieldName, @DataType,<br />
@FieldLength, @Precision, @Scale, @AllowNulls<br />
END</p>
<p>CLOSE CurHistoryTable<br />
DEALLOCATE CurHistoryTable</p>
<p>&#8211; finish history table script with standard history columns<br />
SET @SQLTable = @SQLTable + @TAB + &#8216;[HistoryCreatedOn] [DATETIME] NULL,&#8217; + @CRLF<br />
SET @SQLTable = @SQLTable + @TAB + &#8216;[HistoryCreatedByUserID] [SMALLINT] NULL,&#8217; + @CRLF</p>
<p>SET @SQLTable = @SQLTable + @TAB + &#8216;[HistoryCreatedByUserName] [VARCHAR](30) NULL,&#8217; + @CRLF<br />
SET @SQLTable = @SQLTable + @TAB + &#8216;[HistoryAction] [CHAR](1) NOT NULL&#8217; + @CRLF<br />
SET @SQLTable = @SQLTable + &#8216; )&#8217;</p>
<p>PRINT @SQLTable</p>
<p>&#8211; execute sql script to create history table<br />
EXEC(@SQLTable)</p>
<p>IF @@ERROR &lt;&gt; 0<br />
BEGIN<br />
PRINT &#8216;******************** ERROR CREATING HISTORY TABLE FOR TABLE: &#8216; + @TableName + &#8216; **************************************&#8217;<br />
RETURN -1<br />
END</p>
<p>IF @CreateTrigger = &#8216;Y&#8217;<br />
BEGIN<br />
&#8211; create history trigger<br />
SET @SQLTrigger = &#8216;/************************************************************************************************************&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;Created By: &#8216; + SUSER_SNAME() + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;Created On: &#8216; + @Date + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;Comments: Auto generated trigger&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;***********************************************************************************************/&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;CREATE TRIGGER [Trigger_' + @TableName + '_UpdateDelete] ON DBO.&#8217; + @TableName + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;FOR UPDATE, DELETE&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;AS&#8217; + @CRLF + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;DECLARE @Action CHAR(1)&#8217; + @CRLF + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;IF EXISTS (SELECT &#8216; + @FirstField + &#8216; FROM Inserted)&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;BEGIN&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + @TAB + &#8216;SET @Action = &#8221;U&#8221;&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;END&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;ELSE&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;BEGIN&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + @TAB + &#8216;SET @Action = &#8221;D&#8221;&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;END&#8217; + @CRLF + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;INSERT INTO History_&#8217; + @TableName + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + @TAB + &#8216;(&#8216; + @FieldList + &#8216;, HistoryCreatedOn, HistoryCreatedByUserName, HistoryAction)&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;SELECT &#8216; + @FieldList + &#8216;, GETDATE(), SUSER_SNAME(), @Action&#8217; + @CRLF<br />
SET @SQLTrigger = @SQLTrigger + &#8216;FROM DELETED&#8217;<br />
&#8211;PRINT @SQLTrigger<br />
&#8211; execute sql script to create update/delete trigger<br />
EXEC(@SQLTrigger)</p>
<p>IF @@ERROR &lt;&gt; 0<br />
BEGIN<br />
PRINT &#8216;******************** ERROR CREATING HISTORY TRIGGER FOR TABLE: &#8216; + @TableName + &#8216; **************************************&#8217;<br />
RETURN -1<br />
END</p>
<p>END</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thecodeslinger.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thecodeslinger.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thecodeslinger.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thecodeslinger.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thecodeslinger.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thecodeslinger.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thecodeslinger.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thecodeslinger.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thecodeslinger.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thecodeslinger.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thecodeslinger.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thecodeslinger.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thecodeslinger.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thecodeslinger.wordpress.com/69/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=69&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thecodeslinger.wordpress.com/2011/02/11/auto-generate-history-table-and-corresponding-trigger-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73b96d7881b12bd6c57aeea902b17a16?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Peter Samwel</media:title>
		</media:content>
	</item>
		<item>
		<title>Client Specific Metadata in your MVC Application</title>
		<link>http://thecodeslinger.wordpress.com/2011/01/31/client-specific-metadata-in-your-mvc-application/</link>
		<comments>http://thecodeslinger.wordpress.com/2011/01/31/client-specific-metadata-in-your-mvc-application/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 18:21:52 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">https://thecodeslinger.wordpress.com/2011/01/31/client-specific-metadata-in-your-mvc-application/</guid>
		<description><![CDATA[Recently I was working on a new requirement for one of our systems in which each different client may refer to the same data types differently.&#160; For example, ClientA might refer to roadways as a “Parkways” whereas ClientB and ClientC might refer to them as “Highways”.&#160;&#160; Changes like these obviously aren’t big enough to warrant [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=67&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I was working on a new requirement for one of our systems in which each different client may refer to the same data types differently.&#160; For example, ClientA might refer to roadways as a “Parkways” whereas ClientB and ClientC might refer to them as “Highways”.&#160;&#160; Changes like these obviously aren’t big enough to warrant separate Views for each screen in which data types like these are referenced or mentioned, however sometimes small things like that make a big difference to the client and makes the application feel more in line with their existing business processes.</p>
<p>So, in order to make it so that my application is still a single code-base that will work for all the various clients that might come along and their varying terminologies, here is what I came up with that I think integrates nicely into an existing MVC application and requires only slight modifications.</p>
<p>First off, here is my interface and concrete implementation of a dictionary wrapper that I will use to not only store the key/value pairs which represent the data types but which will be what my Views utilize as an appendage to their ViewModel instances.</p>
<div style="font-family:consolas;background:white;color:black;font-size:10pt;">
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">interface</span> <span style="color:#2b91af;">IMeta</span></p>
<p style="margin:0;">&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#2b91af;">MetaDictionary</span> Meta { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;">&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">MetaDictionary</span></p>
<p style="margin:0;">&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">private</span> <span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:blue;">string</span>, <span style="color:#2b91af;">KeyValuePair</span>&lt;<span style="color:blue;">string</span>,<span style="color:blue;">string</span>&gt;&gt; dict;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> MetaDictionary()</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dict = <span style="color:blue;">new</span> <span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:blue;">string</span>, <span style="color:#2b91af;">KeyValuePair</span>&lt;<span style="color:blue;">string</span>, <span style="color:blue;">string</span>&gt;&gt;();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">void</span> Add(<span style="color:blue;">string</span> key, <span style="color:#2b91af;">KeyValuePair</span>&lt;<span style="color:blue;">string</span>,<span style="color:blue;">string</span>&gt; value)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span> (!dict.ContainsKey(key))</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dict.Add(key, value);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">else</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dict[key] = value;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">void</span> Remove(<span style="color:blue;">string</span> key)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span> (dict.ContainsKey(key))</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dict.Remove(key);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">string</span> GetValue(<span style="color:blue;">string</span> key)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span>(dict.ContainsKey(key))</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">return</span> dict[key].Key;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">else</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">return</span> <span style="color:#a31515;">&quot;&quot;</span>;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">string</span> GetValueAbbrev(<span style="color:blue;">string</span> key)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span>(dict.ContainsKey(key))</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">return</span> dict[key].Value;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">else</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">return</span> <span style="color:#a31515;">&quot;&quot;</span>;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;&#160;&#160; }</p>
</p></div>
<p>Next, I need to write an action filter that will fetch the appropriate key/value pairs relevant to the currently logged in user and which client they belong to.&#160; Please note that this is where your logic goes to determine A) where to retrieve your key/value pairs to begin with (DB, XML, config file, etc) and B) what differentiating factor(s) you might have which would determine how your application knows each client.&#160; In my situation, I currently have a variable which would store the ClientID in a pre-existing injected Session wrapper.&#160; Also note, I’m using <a href="http://structuremap.net/structuremap/" target="_blank">StructureMap</a> as my DI container.</p>
<div style="font-family:consolas;background:white;color:black;font-size:10pt;">
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">ClientMetaAttribute</span> : <span style="color:#2b91af;">ActionFilterAttribute</span>, <span style="color:#2b91af;">IActionFilter</span></p>
<p style="margin:0;">&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">private</span> <span style="color:blue;">readonly</span> <span style="color:#2b91af;">IControllerDependency</span> Services;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> ClientMetaAttribute():<span style="color:blue;">this</span>(<span style="color:#2b91af;">ObjectFactory</span>.GetInstance&lt;<span style="color:#2b91af;">IControllerDependency</span>&gt;())</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> ClientMetaAttribute(<span style="color:#2b91af;">IControllerDependency</span> service)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Services = service;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> OnResultExecuting(<span style="color:#2b91af;">ResultExecutingContext</span> filterContext)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">//Lookup all appropriate attributes.</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">int</span>? clientid = Services.SessionHandler.ClientID;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">var</span> metadict = <span style="color:blue;">new</span> <span style="color:#2b91af;">MetaDictionary</span>();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">//FILL YOUR META dictionary here!!</span></p>
<p style="margin:0;">&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ((<span style="color:#2b91af;">IMeta</span>)((<span style="color:#2b91af;">ViewResultBase</span>)filterContext.Result).ViewData.Model).Meta = metadict;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">base</span>.OnResultExecuting(filterContext);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> OnResultExecuted(<span style="color:#2b91af;">ResultExecutedContext</span> filterContext)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">base</span>.OnResultExecuted(filterContext);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>Now, once I’ve implemented IMeta in the ViewModel used for my action method:</p>
<div style="font-family:consolas;background:white;color:black;font-size:10pt;">
<p style="margin:0;">&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">ClientViewModel</span>: <span style="color:#2b91af;">IMeta</span></p>
<p style="margin:0;">&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:#2b91af;">MetaDictionary</span> Meta { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:green;">//Rest of model class definition…</span></p>
<p style="margin:0;">&#160;&#160;&#160; }</p>
</p></div>
<p>Then I simply need to update any hardcoded references within View(s) which use this ViewModel such as:    </p>
<p>&lt;h4&gt;&lt;%: Model.Meta.GetValue(&quot;roadname&quot;) %&gt; (&lt;%: Model.Meta.GetValueAbbrev(&quot;roadname&quot;) %&gt;)&lt;/h4&gt;</p>
<p>Which for ClientA will give me </p>
<h4>Parkway (PKWY)</h4>
<p>and for for ClientB &amp; ClientC will give </p>
<h4><font size="3">Highway (HWY)</font></h4>
<p> Obviously this could be expanded in terms of the dictionary value storage to include a host of other information that may be specific to your needs, however this example shows the intent.&#160; Additionally, another enhancement would be to extend this for use in a localization scenario where multiple languages may be necessary as well.&#160;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thecodeslinger.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thecodeslinger.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thecodeslinger.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thecodeslinger.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thecodeslinger.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thecodeslinger.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thecodeslinger.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thecodeslinger.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thecodeslinger.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thecodeslinger.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thecodeslinger.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thecodeslinger.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thecodeslinger.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thecodeslinger.wordpress.com/67/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=67&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thecodeslinger.wordpress.com/2011/01/31/client-specific-metadata-in-your-mvc-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73b96d7881b12bd6c57aeea902b17a16?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Peter Samwel</media:title>
		</media:content>
	</item>
		<item>
		<title>Expression based RenderAction HtmlHelper extension (with Authorization)</title>
		<link>http://thecodeslinger.wordpress.com/2010/12/28/expression-based-renderaction-htmlhelper-extension/</link>
		<comments>http://thecodeslinger.wordpress.com/2010/12/28/expression-based-renderaction-htmlhelper-extension/#comments</comments>
		<pubDate>Tue, 28 Dec 2010 17:39:22 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">https://thecodeslinger.wordpress.com/2010/12/28/expression-based-renderaction-htmlhelper-extension/</guid>
		<description><![CDATA[&#160;&#160;&#160;&#160;&#160;&#160;&#160; public static void RenderAuthorizedAction&#60;TController&#62;(this HtmlHelper helper, Expression&#60;Action&#60;TController&#62;&#62; action) where TController : Controller &#160;&#160;&#160;&#160;&#160;&#160;&#160; { &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; var routeValuesFromExpression = Microsoft.Web.Mvc.Internal.ExpressionHelper.GetRouteValuesFromExpression&#60;TController&#62;(action); &#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if(helper.IsAuthorized(action)) &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; helper.RenderAction(routeValuesFromExpression[&#34;Action&#34;].ToString(), routeValuesFromExpression); &#160;&#160;&#160;&#160;&#160;&#160;&#160; } &#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160; public static bool IsAuthorized&#60;TController&#62;(this HtmlHelper helper, Expression&#60;Action&#60;TController&#62;&#62; action) &#160;&#160;&#160;&#160;&#160;&#160;&#160; { &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; var call = action.Body as MethodCallExpression; &#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (call == null) return false; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=63&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[</p>
<div style="font-family:consolas;background:white;color:black;font-size:10pt;">
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> RenderAuthorizedAction&lt;TController&gt;(<span style="color:blue;">this</span> <span style="color:#2b91af;">HtmlHelper</span> helper, <span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Action</span>&lt;TController&gt;&gt; action) <span style="color:blue;">where</span> TController : <span style="color:#2b91af;">Controller</span></p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">var</span> routeValuesFromExpression = Microsoft.Web.Mvc.Internal.<span style="color:#2b91af;">ExpressionHelper</span>.GetRouteValuesFromExpression&lt;TController&gt;(action);</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span>(helper.IsAuthorized(action))</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; helper.RenderAction(routeValuesFromExpression[<span style="color:#a31515;">&quot;Action&quot;</span>].ToString(), routeValuesFromExpression);</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;">&#160;</p>
<div style="font-family:consolas;background:white;color:black;font-size:10pt;">
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">bool</span> IsAuthorized&lt;TController&gt;(<span style="color:blue;">this</span> <span style="color:#2b91af;">HtmlHelper</span> helper, <span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Action</span>&lt;TController&gt;&gt; action)</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">var</span> call = action.Body <span style="color:blue;">as</span> <span style="color:#2b91af;">MethodCallExpression</span>;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span> (call == <span style="color:blue;">null</span>) <span style="color:blue;">return</span> <span style="color:blue;">false</span>;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">var</span> authorizeAttributes = call.GetAttributes&lt;<span style="color:#2b91af;">IAuthorizationFilter</span>&gt;();</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">if</span> (authorizeAttributes.Length == 0) <span style="color:blue;">return</span> <span style="color:blue;">true</span>;</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">var</span> controllerContext = helper.ViewContext.Controller.ControllerContext;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">var</span> controllerDescriptor = <span style="color:blue;">new</span> <span style="color:#2b91af;">ReflectedControllerDescriptor</span>(<span style="color:blue;">typeof</span>(TController));</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">var</span> actionDescriptor = <span style="color:blue;">new</span> <span style="color:#2b91af;">ReflectedActionDescriptor</span>(call.Method, call.Method.Name, controllerDescriptor);</p>
<p style="margin:0;">&#160;</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">return</span> authorizeAttributes.All(a =&gt; IsAuthorized(a, controllerContext, actionDescriptor));</p>
<p style="margin:0;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
</p></div>
<p>I’m sure this is coming in MVC 3, however I needed it now and it’s fairly straightforward.&#160; To use it, simply call from your View as:   </p>
<p>&lt;% Html.RenderAuthorizedAction&lt;MyController&gt;(a =&gt; a.MyActionMethod()); %&gt;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thecodeslinger.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thecodeslinger.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thecodeslinger.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thecodeslinger.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thecodeslinger.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thecodeslinger.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thecodeslinger.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thecodeslinger.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thecodeslinger.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thecodeslinger.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thecodeslinger.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thecodeslinger.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thecodeslinger.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thecodeslinger.wordpress.com/63/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=63&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thecodeslinger.wordpress.com/2010/12/28/expression-based-renderaction-htmlhelper-extension/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73b96d7881b12bd6c57aeea902b17a16?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Peter Samwel</media:title>
		</media:content>
	</item>
		<item>
		<title>Funniest thing I&#8217;ve read in a while&#8230;.</title>
		<link>http://thecodeslinger.wordpress.com/2009/03/30/funniest-thing-ive-read-in-a-while/</link>
		<comments>http://thecodeslinger.wordpress.com/2009/03/30/funniest-thing-ive-read-in-a-while/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 22:47:31 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://thecodeslinger.wordpress.com/?p=47</guid>
		<description><![CDATA[This isn&#8217;t tech related&#8230;but I am crying here after reading this.  No idea if it&#8217;s true or not, but it gave me a great laugh on a day when I needed it. First Date Gone Horribly Wrong<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=47&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This isn&#8217;t tech related&#8230;but I am crying here after reading this.  No idea if it&#8217;s true or not, but it gave me a great laugh on a day when I needed it.</p>
<p><a href="http://thecodeslinger.files.wordpress.com/2009/03/crglst1.pdf">First Date Gone Horribly Wrong</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thecodeslinger.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thecodeslinger.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thecodeslinger.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thecodeslinger.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thecodeslinger.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thecodeslinger.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thecodeslinger.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thecodeslinger.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thecodeslinger.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thecodeslinger.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thecodeslinger.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thecodeslinger.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thecodeslinger.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thecodeslinger.wordpress.com/47/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=47&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thecodeslinger.wordpress.com/2009/03/30/funniest-thing-ive-read-in-a-while/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73b96d7881b12bd6c57aeea902b17a16?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Peter Samwel</media:title>
		</media:content>
	</item>
		<item>
		<title>Color Transitions in Winform Controls</title>
		<link>http://thecodeslinger.wordpress.com/2008/11/17/color-transitions-in-winform-controls/</link>
		<comments>http://thecodeslinger.wordpress.com/2008/11/17/color-transitions-in-winform-controls/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 21:02:41 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Tuples]]></category>
		<category><![CDATA[Winform]]></category>

		<guid isPermaLink="false">http://thecodeslinger.wordpress.com/2008/11/17/color-transitions-in-winform-controls/</guid>
		<description><![CDATA[If you&#8217;ve ever used web-sites like the hosted WordPress blogs (like this one!) and others, you&#8217;ve noticed the new sweet hotness is to have screens give feedback on success or failure of an operation by showing color gradient transitions.  The most common is simply &#8220;Green to White&#8221; (success!) or &#8220;Red to Pink&#8221; (failure!).  This is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=45&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever used web-sites like the hosted WordPress blogs (like this one!) and others, you&#8217;ve noticed the new <em>sweet hotness</em> is to have screens give feedback on success or failure of an operation by showing color gradient transitions.  The most common is simply &#8220;Green to White&#8221; (success!) or &#8220;Red to Pink&#8221; (failure!).  This is a quick way to let the user know they can move along and do more work or whether they had an issue.  Granted, more detailed logging and/or messages are needed on the &#8220;fail&#8221; side of things, however since code slingers like us write perfect code all the time&#8230;.we are just gonna go with quick feedback for now <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>So, being as I&#8217;ve only started in the last few months writing winform/WPF apps, I thought there must be an easy way to do these gradients on forms and other controls quite easily.  No so.  Sure, there are <em>ForeColor</em> and <em>BackColor</em> properties, however transitioning in a gradient-like fashion from one color to the next isn&#8217;t &#8220;built in&#8221; in any way that I could tell.  NOTE: <em>Yes, I&#8217;m well aware of the fact that WPF is going to take over the world.  But for now this is a Winform app, and until such time as WPF becomes the de-facto standard, it will remain as such.  Accordingly, please don&#8217;t comment about how WPF can handle this with ease.  I know.</em></p>
<p>So here&#8217;s what I came up with:</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;">        <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> UpdateColor(<span style="color:#2b91af;">Control</span> ctl, <span style="color:#2b91af;">Color</span> from, <span style="color:#2b91af;">Color</span> to)</p>
<p style="margin:0;">        {</p>
<p style="margin:0;">            <span style="color:blue;">int</span> step = 1; <span style="color:green;">//Increase if you want the transition to be faster.</span></p>
<p style="margin:0;">            <span style="color:blue;">var</span> fromT = <span style="color:blue;">new</span> <span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>, <span style="color:blue;">int</span>, <span style="color:blue;">int</span>&gt;(</p>
<p style="margin:0;">                <span style="color:#2b91af;">Convert</span>.ToInt32(from.R),</p>
<p style="margin:0;">                <span style="color:#2b91af;">Convert</span>.ToInt32(from.G),</p>
<p style="margin:0;">                <span style="color:#2b91af;">Convert</span>.ToInt32(from.B));</p>
<p style="margin:0;">            <span style="color:blue;">var</span> toT = <span style="color:blue;">new</span> <span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>, <span style="color:blue;">int</span>, <span style="color:blue;">int</span>&gt;(</p>
<p style="margin:0;">                <span style="color:#2b91af;">Convert</span>.ToInt32(to.R),</p>
<p style="margin:0;">                <span style="color:#2b91af;">Convert</span>.ToInt32(to.G),</p>
<p style="margin:0;">                <span style="color:#2b91af;">Convert</span>.ToInt32(to.B));</p>
<p style="margin:0;">            <span style="color:blue;">var</span> curT = <span style="color:blue;">new</span> <span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">int</span>, <span style="color:blue;">int</span>, <span style="color:blue;">int</span>&gt;(fromT);</p>
<p style="margin:0;">            <span style="color:blue;">var</span> done = <span style="color:blue;">new</span> <span style="color:#2b91af;">Tuple</span>&lt;<span style="color:blue;">bool</span>, <span style="color:blue;">bool</span>, <span style="color:blue;">bool</span>&gt;(<span style="color:blue;">false</span>, <span style="color:blue;">false</span>, <span style="color:blue;">false</span>);</p>
<p style="margin:0;"> </p>
<p style="margin:0;">            <span style="color:blue;">while</span> (<span style="color:blue;">true</span>)</p>
<p style="margin:0;">            {</p>
<p style="margin:0;">                <span style="color:green;">//Red</span></p>
<p style="margin:0;">                <span style="color:blue;">if</span> (curT.First &gt;= toT.First)</p>
<p style="margin:0;">                    done.First = <span style="color:blue;">true</span>;</p>
<p style="margin:0;">                <span style="color:blue;">else</span></p>
<p style="margin:0;">                {</p>
<p style="margin:0;">                    <span style="color:blue;">var</span> tempval = curT.First &lt; toT.First ? curT.First += step : curT.First -= step;</p>
<p style="margin:0;">                    curT.First = tempval &gt; toT.First ? toT.First : tempval;</p>
<p style="margin:0;">                }</p>
<p style="margin:0;">                <span style="color:green;">//Green</span></p>
<p style="margin:0;">                <span style="color:blue;">if</span> (curT.Second &gt;= toT.Second)</p>
<p style="margin:0;">                    done.Second = <span style="color:blue;">true</span>;</p>
<p style="margin:0;">                <span style="color:blue;">else</span></p>
<p style="margin:0;">                {</p>
<p style="margin:0;">                    <span style="color:blue;">var</span> tempval = curT.Second &lt; toT.Second ? curT.Second += step : curT.Second -= step;</p>
<p style="margin:0;">                    curT.Second = tempval &gt; toT.Second ? toT.Second : tempval;</p>
<p style="margin:0;">                }</p>
<p style="margin:0;">                <span style="color:green;">//Blue</span></p>
<p style="margin:0;">                <span style="color:blue;">if</span> (curT.Third &gt;= toT.Third)</p>
<p style="margin:0;">                    done.Third = <span style="color:blue;">true</span>;</p>
<p style="margin:0;">                <span style="color:blue;">else</span></p>
<p style="margin:0;">                {</p>
<p style="margin:0;">                    <span style="color:blue;">var</span> tempval = curT.Third &lt; toT.Third ? curT.Third += step : curT.Third -= step;</p>
<p style="margin:0;">                    curT.Third = tempval &gt; toT.Third ? toT.Third : tempval;</p>
<p style="margin:0;">                }</p>
<p style="margin:0;"> </p>
<p style="margin:0;">                <span style="color:blue;">if</span> (done.First &amp;&amp; done.Second &amp;&amp; done.Third)</p>
<p style="margin:0;">                {</p>
<p style="margin:0;">                    ctl.BackColor = to;</p>
<p style="margin:0;">                    ctl.Refresh();</p>
<p style="margin:0;">                    <span style="color:blue;">break</span>;</p>
<p style="margin:0;">                }</p>
<p style="margin:0;">                ctl.BackColor = <span style="color:#2b91af;">Color</span>.FromArgb(curT.First, curT.Second, curT.Third);</p>
<p style="margin:0;">                ctl.Refresh();</p>
<p style="margin:0;">            }</p>
<p style="margin:0;">        }</p>
</div>
<p>So, basically I&#8217;m just getting the integer values of the RGB color scheme for the <em>from </em>and <em>to</em> colors passed in, looping through adding/subtracting from the start values (from) until I get to the end values (to), refreshing the control along the way.  Not too terribly difficult, but interesting none-the-less and quite a nice little UI feature that doesn&#8217;t look like most winform applications.</p>
<p>Also, note I&#8217;ve added a step variable that you can tweak to adjust how fast or slow the transition occurs.  It&#8217;s a pretty significant jump from say 1 to 3, so if you want more granular timing, you could adjust the int storage to decimal and account for parts of steps (such as += 0.3).  Or you could implement a true timing system that would force the transition to take place within a specific time construct (perhaps the next article on this topic??).  But for my needs currently, this is more than sufficient.</p>
<p>And of course, I&#8217;m using my handy little <em>Tuple&lt;T1,T2,T3&gt;</em> class, since tuples (or at least the concept of them) rock.  Hint hint&#8230;please make this concept a part of C# 4.0 Anders&#8230;puuuleeeeeease?</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;">    <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Tuple</span>&lt;T1,T2,T3&gt;</p>
<p style="margin:0;">    {</p>
<p style="margin:0;">        <span style="color:blue;">public</span> T1 First { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>;}</p>
<p style="margin:0;">        <span style="color:blue;">public</span> T2 Second { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;">        <span style="color:blue;">public</span> T3 Third { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;"> </p>
<p style="margin:0;">        <span style="color:blue;">public</span> Tuple(T1 t, T2 k, T3 l)</p>
<p style="margin:0;">        {</p>
<p style="margin:0;">            First = t;</p>
<p style="margin:0;">            Second = k;</p>
<p style="margin:0;">            Third = l;</p>
<p style="margin:0;">        }</p>
<p style="margin:0;">        <span style="color:blue;">public</span> Tuple(<span style="color:#2b91af;">Tuple</span>&lt;T1,T2,T3&gt; t)</p>
<p style="margin:0;">        {</p>
<p style="margin:0;">            First = t.First;</p>
<p style="margin:0;">            Second = t.Second;</p>
<p style="margin:0;">            Third = t.Third;</p>
<p style="margin:0;">        }</p>
<p style="margin:0;">    }</p>
<p style="margin:0;"> </p>
<p style="margin:0;">Til next time&#8230;.<em>keep on slangin!</em></p>
<p style="margin:0;"> </p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thecodeslinger.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thecodeslinger.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thecodeslinger.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thecodeslinger.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thecodeslinger.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thecodeslinger.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thecodeslinger.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thecodeslinger.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thecodeslinger.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thecodeslinger.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thecodeslinger.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thecodeslinger.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thecodeslinger.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thecodeslinger.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=45&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thecodeslinger.wordpress.com/2008/11/17/color-transitions-in-winform-controls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73b96d7881b12bd6c57aeea902b17a16?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Peter Samwel</media:title>
		</media:content>
	</item>
		<item>
		<title>Impersonation &amp; IDisposable</title>
		<link>http://thecodeslinger.wordpress.com/2008/11/12/impersonation-idisposable/</link>
		<comments>http://thecodeslinger.wordpress.com/2008/11/12/impersonation-idisposable/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 17:25:07 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IDisposable]]></category>
		<category><![CDATA[Impersonation]]></category>

		<guid isPermaLink="false">http://thecodeslinger.wordpress.com/2008/11/12/impersonation-idisposable/</guid>
		<description><![CDATA[I&#8217;ve used an impersonation helper class for a while now, basically to encapsulate changing the current identity context while executing certain code.&#160; This worked well, however I always got sick of having to remember to call the &#8220;undo()&#8221; method of my current implementation once I was done with executing my code under whatever login I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=44&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve used an impersonation helper class for a while now, basically to encapsulate changing the current identity context while executing certain code.&nbsp; This worked well, however I always got sick of having to remember to call the &#8220;undo()&#8221; method of my current implementation once I was done with executing my code under whatever login I was impersonating.</p>
<p>So, I updated the class to implement <em>IDisposable</em> so that I can now utilize this within the <em>using()</em> construct provided in C#.&nbsp;&nbsp; Here&#8217;s the class in it&#8217;s entirety:</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:blue;">using</span> System;</p>
<p style="margin:0;"><span style="color:blue;">using</span> System.Runtime.InteropServices;</p>
<p style="margin:0;"><span style="color:blue;">using</span> System.Security.Principal;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;"><span style="color:blue;">namespace</span> Support.Win32</p>
<p style="margin:0;">{</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Impersonate</span> : <span style="color:#2b91af;">IDisposable</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">const</span> <span style="color:blue;">int</span> LOGON32_LOGON_INTERACTIVE = 2;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">const</span> <span style="color:blue;">int</span> LOGON32_PROVIDER_DEFAULT = 0;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">WindowsImpersonationContext</span> impersonationContext;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> Impersonate()</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">//Default login is used.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">this</span>.impersonateValidUser(<span style="color:#a31515;">&#8220;username&#8221;</span>, <span style="color:#a31515;">&#8220;domain&#8221;</span>, <span style="color:#a31515;">&#8220;password&#8221;</span>);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> Impersonate(<span style="color:blue;">string</span> username, <span style="color:blue;">string</span> domain, <span style="color:blue;">string</span> password)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">this</span>.impersonateValidUser(username, domain, password);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">DllImport</span>(<span style="color:#a31515;">"advapi32.dll"</span>)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">extern</span> <span style="color:blue;">int</span> LogonUserA(<span style="color:#2b91af;">String</span> lpszUserName,</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">String</span> lpszDomain,</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">String</span> lpszPassword,</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">int</span> dwLogonType,</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">int</span> dwLogonProvider,</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">ref</span> <span style="color:#2b91af;">IntPtr</span> phToken);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">DllImport</span>(<span style="color:#a31515;">"advapi32.dll"</span>, CharSet = <span style="color:#2b91af;">CharSet</span>.Auto, SetLastError = <span style="color:blue;">true</span>)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">extern</span> <span style="color:blue;">int</span> DuplicateToken(<span style="color:#2b91af;">IntPtr</span> hToken,</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">int</span> impersonationLevel,</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">ref</span> <span style="color:#2b91af;">IntPtr</span> hNewToken);</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">DllImport</span>(<span style="color:#a31515;">"advapi32.dll"</span>, CharSet = <span style="color:#2b91af;">CharSet</span>.Auto, SetLastError = <span style="color:blue;">true</span>)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">extern</span> <span style="color:blue;">bool</span> RevertToSelf();</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">DllImport</span>(<span style="color:#a31515;">"kernel32.dll"</span>, CharSet = <span style="color:#2b91af;">CharSet</span>.Auto)]</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">extern</span> <span style="color:blue;">bool</span> CloseHandle(<span style="color:#2b91af;">IntPtr</span> handle);</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">bool</span> impersonateValidUser(<span style="color:blue;">string</span> userName, <span style="color:blue;">string</span> domain, <span style="color:blue;">string</span> password)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">WindowsIdentity</span> tempWindowsIdentity;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">IntPtr</span> token = <span style="color:#2b91af;">IntPtr</span>.Zero;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">IntPtr</span> tokenDuplicate = <span style="color:#2b91af;">IntPtr</span>.Zero;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (RevertToSelf())</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LOGON32_PROVIDER_DEFAULT, <span style="color:blue;">ref</span> token) != 0)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (DuplicateToken(token, 2, <span style="color:blue;">ref</span> tokenDuplicate) != 0)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tempWindowsIdentity = <span style="color:blue;">new</span> <span style="color:#2b91af;">WindowsIdentity</span>(tokenDuplicate);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; impersonationContext = tempWindowsIdentity.Impersonate();</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (impersonationContext != <span style="color:blue;">null</span>)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CloseHandle(token);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CloseHandle(tokenDuplicate);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> <span style="color:blue;">true</span>;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (token != <span style="color:#2b91af;">IntPtr</span>.Zero)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CloseHandle(token);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (tokenDuplicate != <span style="color:#2b91af;">IntPtr</span>.Zero)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CloseHandle(tokenDuplicate);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> <span style="color:blue;">false</span>;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">private</span> <span style="color:blue;">void</span> undoImpersonation()</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; impersonationContext.Undo();</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">void</span> Dispose()</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dispose(<span style="color:blue;">true</span>);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">GC</span>.SuppressFinalize(<span style="color:blue;">this</span>);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~Impersonate()</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dispose(<span style="color:blue;">false</span>);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">protected</span> <span style="color:blue;">virtual</span> <span style="color:blue;">void</span> Dispose(<span style="color:blue;">bool</span> disposing)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span>(disposing)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">//Dispose of managed resources.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span>(impersonationContext != <span style="color:blue;">null</span>)</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">this</span>.undoImpersonation();</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; impersonationContext.Dispose();</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; impersonationContext = <span style="color:blue;">null</span>;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">}</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">Basically, now my code can use the following construct in order to execute code in an impersonated context:</p>
<p style="margin:0;">&nbsp;</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">using</span>(<span style="color:blue;">var</span> imp = <span style="color:blue;">new</span> <span style="color:#2b91af;">Impersonate</span>(<span style="color:#a31515;">&#8220;username&#8221;</span>, <span style="color:#a31515;">&#8220;domain&#8221;</span>, <span style="color:#a31515;">&#8220;password&#8221;</span>))</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green;">//Code that I want to run under this account.</span></p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
</div>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">&nbsp;</div>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">That&#8217;s it! Now I can be lazy and not have to worry about everything getting &#8220;cleaned up&#8221;.&nbsp; What&#8217;s more, is I&#8217;ve made private the actual calls to <em>impersonateValidUser</em> expressly so that it forces me to use this with the using construct.&nbsp; </div>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">&nbsp;</div>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">Til next time&#8230;..<strong><em>keep on slangin!</em></strong></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thecodeslinger.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thecodeslinger.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thecodeslinger.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thecodeslinger.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thecodeslinger.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thecodeslinger.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thecodeslinger.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thecodeslinger.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thecodeslinger.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thecodeslinger.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thecodeslinger.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thecodeslinger.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thecodeslinger.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thecodeslinger.wordpress.com/44/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=44&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thecodeslinger.wordpress.com/2008/11/12/impersonation-idisposable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73b96d7881b12bd6c57aeea902b17a16?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Peter Samwel</media:title>
		</media:content>
	</item>
		<item>
		<title>LINQKit: PredicateBuilder&lt;T&gt; Goodness</title>
		<link>http://thecodeslinger.wordpress.com/2008/10/28/linqkit-predicatebuildert-goodness/</link>
		<comments>http://thecodeslinger.wordpress.com/2008/10/28/linqkit-predicatebuildert-goodness/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 20:52:19 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[PredicateBuilder]]></category>

		<guid isPermaLink="false">http://thecodeslinger.wordpress.com/2008/10/28/linqkit-predicatebuildert-goodness/</guid>
		<description><![CDATA[Remember the good ol&#8217; days when you wanted to create a query that had dependencies that were unknown at runtime? You&#8217;d create a nice StringBuilder instance, and start building your own SQL to handle the complex logic of ever possible permutation that you allowed via your UI.  Or you create the hairiest stored procedure known [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=36&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Remember the good ol&#8217; days when you wanted to create a query that had dependencies that were unknown at runtime? You&#8217;d create a nice<em> StringBuilder</em> instance, and start building your own SQL to handle the complex logic of ever possible permutation that you allowed via your UI.  Or you create the hairiest stored procedure known to man with umpteen variables and then tried not only to not screw it up internally, but to make every scenario as fast as possible.  Yeah, those days were fun weren&#8217;t they?</p>
<p>With LINQ, such tasks are still moderately tough, but now that I&#8217;ve found the very nice <em>PredicateBuilder&lt;T&gt;</em> class that comes in the free <a href="http://www.albahari.com/nutshell/linqkit.aspx">LINQKit</a>, the ease with which one can create dynamic SQL calls is significantly easier. </p>
<p>As an example, supposed you have a table which contains a description column.  And you want to implement a sort of &#8220;smart search&#8221; on that column such that if your user types in:</p>
<p>Include: Dog Cat<br />
Exclude: Hamster</p>
<p>you code translates that into a query such as:</p>
<p>SELECT * FROM Pets<br />
WHERE Description LIKE &#8216;%Dog%&#8217;<br />
AND Description LIKE &#8216;%Cat%&#8217;<br />
AND Description NOT LIKE &#8216;%Hamster%&#8217;</p>
<p>Behold, <em>PredicateBuilder&lt;T&gt;</em>.</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;">            <span style="color:green;">//First get a list of keywords that match the description entered.</span></p>
<p style="margin:0;">            <span style="color:blue;">string</span>[] parts = txtInclude.Text.Split(<span style="color:blue;">new</span>[] {<span style="color:#a31515;">&#8216; &#8216;</span>});</p>
<p style="margin:0;">            <span style="color:blue;">string</span>[] noparts = <span style="color:blue;">null</span>;</p>
<p style="margin:0;">            <span style="color:blue;">if</span>(txtButNot.Text.Trim().Length &gt; 0)</p>
<p style="margin:0;">                noparts = txtExclude.Text.Trim().Split(<span style="color:blue;">new</span>[] {<span style="color:#a31515;">&#8216; &#8216;</span>});</p>
<p style="margin:0;"> </p>
<p style="margin:0;">            <span style="color:blue;">var</span> pred = <span style="color:#2b91af;">PredicateBuilder</span>.True&lt;<span style="color:#2b91af;">Pet</span>&gt;();</p>
<p style="margin:0;">            parts.ForEach(p =&gt; pred = pred.And(pl =&gt; pl.description.Contains(p)));</p>
<p style="margin:0;">            <span style="color:blue;">if</span>(noparts != <span style="color:blue;">null</span>)</p>
<p style="margin:0;">                noparts.ForEach(p =&gt; pred = pred.And(pl =&gt; !pl.description.Contains(p)));</p>
<p style="margin:0;"> </p>
<p style="margin:0;">            <span style="color:blue;">var</span> pets = <span style="color:blue;">from</span> s <span style="color:blue;">in</span> db.Pets.Where(pred)</p>
<p style="margin:0;">                        <span style="color:blue;">select</span> s;</p>
</div>
<p>Notice that it doesn&#8217;t matter what order the user types the keywords in, nor does it matter how many there are.    Obviously this is a trivial example, and I don&#8217;t really have a table that stores pet names <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />   However, I do have some pretty complex requirements to create a way to backtrack and find potential buyers in our system based on previous items they&#8217;ve shown interest in.  Sounds like a nice use for this gem of a find.</p>
<p>Til next time&#8230;.<strong><em>keep on slangin!</em></strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thecodeslinger.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thecodeslinger.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thecodeslinger.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thecodeslinger.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thecodeslinger.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thecodeslinger.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thecodeslinger.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thecodeslinger.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thecodeslinger.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thecodeslinger.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thecodeslinger.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thecodeslinger.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thecodeslinger.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thecodeslinger.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=36&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thecodeslinger.wordpress.com/2008/10/28/linqkit-predicatebuildert-goodness/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73b96d7881b12bd6c57aeea902b17a16?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Peter Samwel</media:title>
		</media:content>
	</item>
		<item>
		<title>LINQDataSource and Optional Parameters</title>
		<link>http://thecodeslinger.wordpress.com/2008/10/22/linqdatasource-and-optional-parameters/</link>
		<comments>http://thecodeslinger.wordpress.com/2008/10/22/linqdatasource-and-optional-parameters/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 19:20:48 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LinqDataSource]]></category>

		<guid isPermaLink="false">http://thecodeslinger.wordpress.com/2008/10/22/linqdatasource-and-optional-parameters/</guid>
		<description><![CDATA[The combination of LINQDataSource and GridView makes a nice quick way to provide basic search capabilities for your users.  Providing a user the ability to search on multiple criteria for a single entity is quick and painless.  However, I ran into an issue where one of the columns I was searching by allowed NULL in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=35&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The combination of LINQDataSource and GridView makes a nice quick way to provide basic search capabilities for your users.  Providing a user the ability to search on multiple criteria for a single entity is quick and painless.  However, I ran into an issue where one of the columns I was searching by allowed <em>NULL</em> in the table. </p>
<p>My fields looked something like:</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;">                <span style="color:blue;">&lt;</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">                    <span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span>Company:<span style="color:red;">&amp;nbsp;</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">                    <span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TextBox</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;txtCompNameSearch&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;&gt;&lt;/</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TextBox</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">                <span style="color:blue;">&lt;/</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">                <span style="color:blue;">&lt;</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">                    <span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span>Phone: <span style="color:red;">&amp;nbsp;</span><span style="color:blue;">&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">                    <span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TextBox</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;txtPhoneSearch&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;&gt;&lt;/</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TextBox</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">                <span style="color:blue;">&lt;/</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
</div>
<p>My LINQDataSource:</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;"> </p>
<p style="margin:0;">    <span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">LinqDataSource</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;ldsCompanies&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span></p>
<p style="margin:0;">        <span style="color:red;">ContextTypeName</span><span style="color:blue;">=&#8221;DataContext&#8221;</span></p>
<p style="margin:0;">        <span style="color:red;">TableName</span><span style="color:blue;">=&#8221;REG_Companies&#8221;</span> <span style="color:red;">EnableUpdate</span><span style="color:blue;">=&#8221;True&#8221;</span></p>
<p style="margin:0;">        <span style="color:red;">Where</span><span style="color:blue;">=&#8221;CompanyName.StartsWith(@CompanyName) &amp;amp;&amp;amp; Phone.StartsWith(@PhoneNumber)&#8221;&gt;</span></p>
<p style="margin:0;">        <span style="color:blue;">&lt;</span><span style="color:#a31515;">WhereParameters</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">            <span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">ControlParameter</span> <span style="color:red;">ConvertEmptyStringToNull</span><span style="color:blue;">=&#8221;false&#8221;</span> <span style="color:red;">ControlID</span><span style="color:blue;">=&#8221;txtCompNameSearch&#8221;</span> <span style="color:red;">Name</span><span style="color:blue;">=&#8221;CompanyName&#8221;</span></p>
<p style="margin:0;">                <span style="color:red;">PropertyName</span><span style="color:blue;">=&#8221;Text&#8221;</span> <span style="color:red;">Type</span><span style="color:blue;">=&#8221;String&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="margin:0;">            <span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">ControlParameter</span> <span style="color:red;">ConvertEmptyStringToNull</span><span style="color:blue;">=&#8221;false&#8221;</span> <span style="color:red;">ControlID</span><span style="color:blue;">=&#8221;txtPhoneSearch&#8221;</span> <span style="color:red;">Name</span><span style="color:blue;">=&#8221;PhoneNumber&#8221;</span></p>
<p style="margin:0;">                <span style="color:red;">PropertyName</span><span style="color:blue;">=&#8221;Text&#8221;</span> <span style="color:red;">Type</span><span style="color:blue;">=&#8221;String&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="margin:0;">        <span style="color:blue;">&lt;/</span><span style="color:#a31515;">WhereParameters</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">    <span style="color:blue;">&lt;/</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">LinqDataSource</span><span style="color:blue;">&gt;</span></p>
</div>
<p>If both <em>CompanyName</em> and <em>Phone</em> fields didn&#8217;t allow nulls, this would work just fine.  However, like my scenario, the Phone field (for whatever reason) allowed NULL values in the database, this would still work but if you left <em>txtPhoneSearch</em> empty, it would only return records in which the <em>Phone</em> field was NOT NULL.</p>
<p>So, the next logical thing to do would be to change the <em>ConvertEmptyStringToNull</em> value to <em>true</em> for that parameter.  Ah, but if you do that you get the following error when you try to load the page the code is on:</p>
<p><span></p>
<h2><em>No applicable method &#8216;StartsWith&#8217; exists in type &#8216;String&#8217;</em></h2>
<p></span></p>
<p>So, this is what I came up with to fix it so you can search optionally on columns that allow nulls using the standard ControlParameter in the LINQDataSource.  Leave the <em>ConvertEmptyStringToNull=&#8221;false&#8221;</em> as you would with non-nullable columns.  However, modify your <em>Where</em> clause to the following:</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;">        <span style="color:red;">Where</span><span style="color:blue;">=&#8221;CompanyName.StartsWith(@CompanyName) &amp;amp;&amp;amp; (@PhoneNumber == String.Empty || (@PhoneNumber != String.Empty &amp;amp;&amp;amp; Phone.StartsWith(@PhoneNumber)))&#8221;&gt;</span></p>
</div>
<p><span> </span></p>
<p>You&#8217;re effectively just giving the query an option to exit early if the @PhoneNumber parameter is empty, but if it is not, then you&#8217;re adding the comparison to the dynamic SQL logic.</p>
<p>Til next time&#8230;.<strong><em>keep on slangin!</em></strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thecodeslinger.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thecodeslinger.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thecodeslinger.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thecodeslinger.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thecodeslinger.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thecodeslinger.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thecodeslinger.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thecodeslinger.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thecodeslinger.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thecodeslinger.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thecodeslinger.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thecodeslinger.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thecodeslinger.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thecodeslinger.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=35&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thecodeslinger.wordpress.com/2008/10/22/linqdatasource-and-optional-parameters/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73b96d7881b12bd6c57aeea902b17a16?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Peter Samwel</media:title>
		</media:content>
	</item>
		<item>
		<title>DBML File Issue</title>
		<link>http://thecodeslinger.wordpress.com/2008/10/06/dbml-file-issue/</link>
		<comments>http://thecodeslinger.wordpress.com/2008/10/06/dbml-file-issue/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 19:06:09 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[DBML]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[VS.NET 2008 SP1]]></category>

		<guid isPermaLink="false">http://thecodeslinger.wordpress.com/2008/10/06/dbml-file-issue/</guid>
		<description><![CDATA[If you edit an existing DBML file (e.g. Foo.dbml) and save it, then notice that your entire Foo.designer.cs is gone and you&#8217;re using VS 2008 SP1, then the issue (which it took me quite a while to figure out) is that you have your using statements OUTSIDE of your namespace declaration within the Foo.designer.cs AND [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=33&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you edit an existing DBML file (e.g. Foo.dbml) and save it, then notice that your entire Foo.designer.cs is gone and you&#8217;re using VS 2008 SP1, then the issue (which it took me quite a while to figure out) is that you have your <em>using</em> statements OUTSIDE of your namespace declaration within the Foo.designer.cs AND any other partial class declarations of the Food class. </p>
<p>Absolutely asinine I know.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thecodeslinger.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thecodeslinger.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thecodeslinger.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thecodeslinger.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thecodeslinger.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thecodeslinger.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thecodeslinger.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thecodeslinger.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thecodeslinger.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thecodeslinger.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thecodeslinger.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thecodeslinger.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thecodeslinger.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thecodeslinger.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=33&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thecodeslinger.wordpress.com/2008/10/06/dbml-file-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73b96d7881b12bd6c57aeea902b17a16?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Peter Samwel</media:title>
		</media:content>
	</item>
		<item>
		<title>Randomization &amp; LINQ</title>
		<link>http://thecodeslinger.wordpress.com/2008/09/30/randomization-linq/</link>
		<comments>http://thecodeslinger.wordpress.com/2008/09/30/randomization-linq/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 21:21:50 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Randomization]]></category>

		<guid isPermaLink="false">http://thecodeslinger.wordpress.com/2008/09/30/randomization-linq/</guid>
		<description><![CDATA[Ran into a problem today in which I had a List&#60;T&#62; of items, and I needed to return a number X of them back, but I wanted a random sampling of the original list.  There are plenty of traditional ways to do this, however I figured I&#8217;d learn something new and figure out how to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=32&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ran into a problem today in which I had a List&lt;T&gt; of items, and I needed to return a number X of them back, but I wanted a random sampling of the original list.  There are plenty of traditional ways to do this, however I figured I&#8217;d learn something new and figure out how to do this using LINQ.</p>
<p>Here&#8217;s my solution, which I turned into an extension method for future use&#8230;&#8230;</p>
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<div style="font-size:10pt;background:white;color:black;font-family:courier new;">
<p style="margin:0;">        <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">List</span>&lt;T&gt; GetRandom&lt;T&gt;(<span style="color:blue;">this</span> <span style="color:#2b91af;">List</span>&lt;T&gt; objs, <span style="color:blue;">int</span> count)</p>
<p style="margin:0;">        {</p>
<p style="margin:0;">            <span style="color:blue;">var</span> ret = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;T&gt;();</p>
<p style="margin:0;"> </p>
<p style="margin:0;">            <span style="color:blue;">if</span>(objs.Count() &lt;= count)</p>
<p style="margin:0;">                <span style="color:blue;">return</span> objs.ToList();</p>
<p style="margin:0;"> </p>
<p style="margin:0;">            <span style="color:blue;">var</span> used = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">int</span>&gt;();</p>
<p style="margin:0;">            <span style="color:blue;">var</span> seq = (<span style="color:#2b91af;">Enumerable</span>.Range(1, count)</p>
<p style="margin:0;">                        .Select(a =&gt;</p>
<p style="margin:0;">                            {</p>
<p style="margin:0;">                                <span style="color:blue;">var</span> num = (<span style="color:blue;">from</span> b <span style="color:blue;">in</span> <span style="color:#2b91af;">Enumerable</span>.Range(0, objs.Count()-1)</p>
<p style="margin:0;">                                           <span style="color:blue;">orderby</span> <span style="color:#2b91af;">Guid</span>.NewGuid()</p>
<p style="margin:0;">                                           <span style="color:blue;">where</span> !(<span style="color:blue;">from</span> u <span style="color:blue;">in</span> used</p>
<p style="margin:0;">                                                  <span style="color:blue;">select</span> u).Contains(b)</p>
<p style="margin:0;">                                           <span style="color:blue;">select</span> b).First();</p>
<p style="margin:0;">                                used.Add(num);</p>
<p style="margin:0;">                                <span style="color:blue;">return</span> num;</p>
<p style="margin:0;">                            })).ToList();</p>
<p style="margin:0;"> </p>
<p style="margin:0;">            <span style="color:blue;">foreach</span> (<span style="color:blue;">int</span> s <span style="color:blue;">in</span> seq) { ret.Add(objs[s]); }</p>
<p style="margin:0;">            <span style="color:blue;">return</span> ret;</p>
<p style="margin:0;">        }</p>
</div>
</div>
<p style="margin:0;"> </p>
</div>
</div>
</div>
</div>
<p>Basically the thinking is that since I&#8217;m dealing with randomizing what&#8217;s in the list, I don&#8217;t really care about data specific to the type &#8220;T&#8221;.  I&#8217;m just randomizing the index count from the original list based on how many I want, then using those indexes I get back from the randomization part to pull the actual objects.</p>
<p>The only thing I don&#8217;t like about it is the fact that I have to create a separate variable ( &#8220;used&#8221; ) to keep track of which items in the original list that I&#8217;ve already used. Seems there should be a better way to do this within the LINQ query, but at this point I am drawing a blank on that one.</p>
<p>Also&#8230;did you notice I am doing randomization without even using the System.Random class? Yeeaaa for me! <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>As always&#8230;.<strong>Keep on Slangin!</strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thecodeslinger.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thecodeslinger.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thecodeslinger.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thecodeslinger.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thecodeslinger.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thecodeslinger.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thecodeslinger.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thecodeslinger.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thecodeslinger.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thecodeslinger.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thecodeslinger.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thecodeslinger.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thecodeslinger.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thecodeslinger.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thecodeslinger.wordpress.com&amp;blog=2155620&amp;post=32&amp;subd=thecodeslinger&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thecodeslinger.wordpress.com/2008/09/30/randomization-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/73b96d7881b12bd6c57aeea902b17a16?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Peter Samwel</media:title>
		</media:content>
	</item>
	</channel>
</rss>
