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

<channel>
	<title>Tracelight.ch - Leuchtspur im Internet &#187; LINQ</title>
	<atom:link href="http://www.tracelight.ch/tag/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tracelight.ch</link>
	<description></description>
	<lastBuildDate>Sun, 04 Dec 2011 10:48:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Cast&lt;&gt; und OfType&lt;&gt; Unterschied</title>
		<link>http://www.tracelight.ch/2008/11/03/cast-und-oftype-unterschied/</link>
		<comments>http://www.tracelight.ch/2008/11/03/cast-und-oftype-unterschied/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 20:25:48 +0000</pubDate>
		<dc:creator>Daniel Marbach</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Cast]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Nich generische Collections]]></category>
		<category><![CDATA[Non generic collections]]></category>
		<category><![CDATA[OfType]]></category>

		<guid isPermaLink="false">http://www.tracelight.ch/2008/11/03/cast-und-oftype-unterschied/</guid>
		<description><![CDATA[LINQ bietet extra für den Umgang mit nicht generischen Collections die beiden Extensionmethoden Cast&#60;&#62; und OfType&#60;&#62;. Mit denen ist es möglich den Inhalt einer nicht generischen Collection wie zum Beispiel ArrayList umzuwandeln. Nehmen wir folgendes Beispiel: public class Person &#123; public string Surname &#123; get; set; &#125; public string LastName &#123; get; set; &#125; &#125; [...]]]></description>
			<content:encoded><![CDATA[<p>LINQ bietet extra für den Umgang mit nicht generischen Collections die beiden Extensionmethoden Cast&lt;&gt; und OfType&lt;&gt;. Mit denen ist es möglich den Inhalt einer nicht generischen Collection wie zum Beispiel ArrayList umzuwandeln. Nehmen wir folgendes Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Person <span style="color: #008000;">&#123;</span>   
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Surname  <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> LastName <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
ArrayList list <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ArrayList <span style="color: #008000;">&#123;</span>  
   <span style="color: #008000;">&#123;</span> <span style="color: #008000;">new</span> Person <span style="color: #008000;">&#123;</span> Surname <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Alfons&quot;</span>, LastName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;TheFirst&quot;</span> <span style="color: #008000;">&#125;</span>,   
   <span style="color: #008000;">&#123;</span> <span style="color: #008000;">new</span> Person <span style="color: #008000;">&#123;</span> Surname <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Alfons&quot;</span>, LastName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;TheSecond&quot;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Wie ihr sicherlich wisst bekommt man beim iterieren durch die ArrayList wie oben aufgezeigt immer eine Referenz auf ein System.Object. Mit den beiden LINQ Operatoren Cast&lt;&gt; und OfType&lt;&gt; kann man nun die Objektreferenzen in die konkrete Klasse Person &#8220;casten&#8221; und bekommt bei einem LINQ Query immer ein IEnumerable&lt;Person&gt; zurück. Dies würde folgendermassen aussehen:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Query syntax</span>
var persons <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span> p <span style="color: #0600FF; font-weight: bold;">in</span> list<span style="color: #008000;">.</span><span style="color: #0000FF;">Cast</span><span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
               <span style="color: #0600FF; font-weight: bold;">select</span> p <span style="color: #0600FF; font-weight: bold;">where</span> p<span style="color: #008000;">.</span><span style="color: #0000FF;">LastName</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;TheSecond&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// Method chaining</span>
var persons <span style="color: #008000;">=</span> list<span style="color: #008000;">.</span><span style="color: #0000FF;">Cast</span><span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span>
                        <span style="color: #0600FF; font-weight: bold;">Where</span><span style="color: #008000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p<span style="color: #008000;">.</span><span style="color: #0000FF;">LastName</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;TheSecond&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Doch was ist nun der Unterschied zwischen den beiden Operatoren? Der Unterschied ist eigentlich schnell erklärt. Der Cast&lt;&gt; Operator versucht jedes Element in der Collection zum entsprechenden Typen zu Casten. Ist der Cast nicht erfolgreich so wird eine Exception geworfen und die Ausführung des Queries beendet. Mit dem OfType&lt;&gt; Operator wird versucht das Element zu Casten und wenn es nicht erfolgreich ist, wird einfach das Element in der Collection ausgelassen.</p>
<p>Dies ist vor allem nützlich, da man bei nicht generischen Collections nicht garantieren kann, dass immer die gleichen Elemente in die Colllection abgefüllt werden. So wäre es durchaus zulässig, dass jemand in der obigen Arraylist noch </p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">list<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Ätschbätsch&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

</p>
<p>hinzufügt. Nähme man dann den Cast&lt;&gt; Operator so wurde die Ausführung des Queries scheitern. Bei OfType&lt;&gt; würde das Element &#8220;Ätschbätsch&#8221; einfach nicht berücksichtigt.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tracelight.ch/2008/11/03/cast-und-oftype-unterschied/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallel LINQ</title>
		<link>http://www.tracelight.ch/2008/06/12/parallel-linq/</link>
		<comments>http://www.tracelight.ch/2008/06/12/parallel-linq/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 09:25:44 +0000</pubDate>
		<dc:creator>Daniel Marbach</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Parallel LINQ]]></category>
		<category><![CDATA[PLINQ]]></category>

		<guid isPermaLink="false">http://www.tracelight.ch/2008/06/12/parallel-linq/</guid>
		<description><![CDATA[Auf MSDN ist ein sehr interessanter Artikel &#252;ber Parallel LINQ erschienen. Bei Parallel LINQ handelt es sich um eine API Erweiterung f&#252;r LINQ mit dem es m&#246;glich ist, die Queries optimiert auf mehreren Prozessoren oder Cores auszuf&#252;hren. Ich kann euch diesen Artikel nur w&#228;rmstens empfehlen. Gut sind auch die Punkte im Artikel wo die Autoren [...]]]></description>
			<content:encoded><![CDATA[<p>Auf MSDN ist ein sehr interessanter Artikel &#252;ber Parallel LINQ erschienen. Bei Parallel LINQ handelt es sich um eine API Erweiterung f&#252;r LINQ mit dem es m&#246;glich ist, die Queries optimiert auf mehreren Prozessoren oder Cores auszuf&#252;hren. Ich kann euch diesen Artikel nur w&#228;rmstens empfehlen. Gut sind auch die Punkte im Artikel wo die Autoren klar auf Probleme und Race Conditions eingehen und wie das Exceptionhandling bei mehreren Threads behandelt wird.</p>
<p><a title="http://msdn.microsoft.com/en-us/magazine/cc163329.aspx" href="http://msdn.microsoft.com/en-us/magazine/cc163329.aspx">http://msdn.microsoft.com/en-us/magazine/cc163329.aspx</a></p>
<p>Es gibt dann auch Webcasts zum Downloaden:</p>
<p><a title="http://www.microsoft.com/germany/msdn/webcasts/serien/MSDNWCS-0806-02.mspx" href="http://www.microsoft.com/germany/msdn/webcasts/serien/MSDNWCS-0806-02.mspx">http://www.microsoft.com/germany/msdn/webcasts/serien/MSDNWCS-0806-02.mspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tracelight.ch/2008/06/12/parallel-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

