<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<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/"
	>

<channel>
	<title>linq-to-objects &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://en.wordpress.com/tag/linq-to-objects/</link>
	<description>Feed of posts on WordPress.com tagged "linq-to-objects"</description>
	<pubDate>Wed, 02 Dec 2009 14:25:05 +0000</pubDate>

	<generator>http://en.wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[[it] Usare LINQ con collezioni non generiche]]></title>
<link>http://techobjects.wordpress.com/2009/10/05/it-usare-linq-con-collezioni-non-generiche/</link>
<pubDate>Mon, 05 Oct 2009 16:11:31 +0000</pubDate>
<dc:creator>stevie</dc:creator>
<guid>http://techobjects.wordpress.com/2009/10/05/it-usare-linq-con-collezioni-non-generiche/</guid>
<description><![CDATA[Prima dell&#8217;avvento dei Generics, con la versione 2.0 del framework .NET, le collezioni erano t]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Prima dell&#8217;avvento dei <a title="Generics" href="http://msdn.microsoft.com/en-us/library/512aeb7t.aspx" target="_self">Generics</a>, con la versione 2.0 del framework .NET, le collezioni erano tutte radunate sotto un unico namespace: <a href="http://msdn.microsoft.com/en-us/library/system.collections.aspx"><code>System.Collections</code></a>.</p>
<p>Ora invece abbiamo a disposizione le versioni generiche di molte di esse (e altre ne stanno per arrivare, ora che .NET 4.0 è alle porte). Basta includere il namespace <a href="http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx"><code>System.Collections.Generic</code></a>.</p>
<p>L&#8217;introduzione dei Generics ha determinato, oltre all&#8217;aggiunta di classi che rappresentano le collezioni vere e proprie, anche la comparsa delle controparti generiche delle interfacce già presenti in System.Collection (ad esempio <code>IList&#60;T&#62;</code> per <code>IList</code>, <code>IEnumerable&#60;T&#62;</code> per <code>IEnumerable</code>, <code>ICollection&#60;T&#62;</code> per <code>ICollection&#62;</code>).</p>
<p>Ed è proprio questa la chiave di volta della faccenda: possiamo invocare buona parte degli operatori standard di LINQ su una collezione solo se questa implementa l&#8217;interfaccia <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx">IE<code>numerable&#60;T&#62;</code></a>. E le vecchie collezioni, naturalmente, non la implementano.</p>
<p>Ci vengono in soccorso proprio due degli operatori standard capaci di lavorare con collezioni che non implementano <code>IEnumerable&#60;T&#62;</code>: <a href="http://msdn.microsoft.com/en-us/library/bb341406.aspx"><code>Cast</code></a> e <a href="http://msdn.microsoft.com/en-us/library/bb360913.aspx"><code>OfType</code></a> (aggiunti all&#8217;interfaccia <a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx"><code>IEnumerable</code></a> sfruttando gli <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx">Extension Methods</a>).</p>
<p>Entrambi richiedono un type parameter (racchiuso tra parentesi angolari, come sempre quando abbiamo a che fare con i Generics) e nessun argomento ordinario.</p>
<p>La differenza fondamentale tra i due è questa: <code>Cast</code> opera la conversione verso il tipo specificato e, se non ci riesce, solleva un&#8217;eccezione di tipo <code>InvalidCastException</code>. <code>OfType</code>, invece, agisce in maniera più simile ad un filtro: prova a convertire ciascun elemento della collezione e, se questo non è possibile, semplicemente passa all&#8217;elemento successivo.</p>
<p>Vale la pena ricordare che le collezioni non generiche eseguono il boxing automatico dei valori che aggiungiamo, trattandoli tutti come object. Se da un lato questo offre un certo grado di flessibilità, dall&#8217;altro permette di aggiungere un oggetto di qualsiasi tipo, anche in maniera accidentale.</p>
<p>Ecco il perché dell&#8217;esistenza di questi due metodi che, se escludiamo la diversa gestione di un eventuale errore di conversione, si comportano in maniera perfettamente identica.</p>
<p>La decisione tra i due, come spesso capita, dipende dal contesto. Se vogliamo evitare eccezioni a runtime, che è forse il caso più probabile, useremo <code>OfType</code>. Se, invece, vogliamo essere sicuri che le situazioni anomale non possano sfuggirci, potremmo voler impiegare <code>Cast</code> ed implementare una gestione custom di <code>InvalidCastException</code> (scelta che, specie nel caso di collezioni contenenti grandi numeri di oggetti, inevitabilmente porterà ad un decadimento delle prestazioni).</p>
<p>Una cosa importante da notare è che, dichiarando esplicitamente il tipo della variabile d&#8217;iterazione nella nostra query expression (aggiungendo cioè il tipo di variabile dopo la clausola <code>from</code> e prima del nome della variabile stessa) possiamo far sì che <code>Cast</code> venga richiamato in modo implicito.</p>
<p>Pertanto, questa query LINQ</p>
<blockquote><p><code>var result = from Employee e in someCollection select e;</code></p></blockquote>
<p>e quest&#8217;altra</p>
<blockquote><p><code>var result = from e in someCollection.Cast&#60;Employee&#62;() select e;</code></p></blockquote>
<p>sono perfettamente equivalenti (in particolare, la prima è la forma implicita della seconda).</p>
<p>Per finire, vale la pena osservare che questo è l&#8217;unico caso in cui abbiamo bisogno specificare il tipo della variabile dopo una clausola <code>from</code>: in tutti gli altri casi sarà l&#8217;inferenza di tipo del compilatore a lavorare per noi.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Usando LINQ para gerenciar coleções de objetos]]></title>
<link>http://gilbertohemmings.wordpress.com/2009/09/30/usando-linq-para-gerenciar-colecoes-de-objetos/</link>
<pubDate>Wed, 30 Sep 2009 21:49:18 +0000</pubDate>
<dc:creator>Gilberto Hemmings</dc:creator>
<guid>http://gilbertohemmings.wordpress.com/2009/09/30/usando-linq-para-gerenciar-colecoes-de-objetos/</guid>
<description><![CDATA[Usando LINQ para gerenciar coleções de objetos LINQ (Language INtegrated Query) é um conjunto de rec]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><h3>Usando LINQ para gerenciar coleções de objetos</h3>
<p></p>
<p><strong>LINQ (Language INtegrated Query)</strong> é um conjunto de recursos introduzidos no .NET Framework 3.5 que estendem o C# e o Visual Basic.NET com uma sintaxe para consultas similar à linguagem SQL, além de fornecer bibliotecas de classes para tirar proveito destes recursos.</p>
<p>Existem cinco formas de utilizá-lo:</p>
<ul>
<li>LINQ to Entities</li>
<li>LINQ to SQL</li>
<li>LINQ to XML</li>
<li>LINQ to Objects</li>
<li>LINQ to DataSet</li>
</ul>
<p></p>
<p>Neste artigo usaremos <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" title="LINQ to Objects">LINQ to Objects</a>, que refere-se ao uso de consultas com LINQ em qualquer coleção <strong>IEnumerable</strong> ou <strong>IEnumerable&#60;T&#62;</strong>, ou seja, podemos consultar qualquer coleção enumerável como <strong>List&#60;T&#62;</strong>, <strong>Array</strong>, <strong>Dictionary&#60;TKey, TValue&#62;</strong>, etc com LINQ.</p>
<p>Vamos definir uma classe <strong>Product</strong> com as propriedades ProductID, ProductName, UnitPrice e UnitsInStock:</p>
<div style="margin:15px 0;">
<pre style="font-size:small;color:white;font-family:Consolas,'Courier New',Courier,Monospace;width:100%;margin:0;"><span style="color:#606060;"> 1: </span><span style="color:#0000ff;">public class</span> <span style="color:DarkCyan;">Product</span>
<span style="color:#606060;"> 2: </span>{
<span style="color:#606060;"> 3: </span>    <span style="color:#0000ff;">public int</span> ProductID { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }
<span style="color:#606060;"> 4: </span>    <span style="color:#0000ff;">public string</span> ProductName { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }
<span style="color:#606060;"> 5: </span>    <span style="color:#0000ff;">decimal</span> UnitPrice { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }
<span style="color:#606060;"> 6: </span>    <span style="color:#0000ff;">public short</span> UnitsInStock { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }
<span style="color:#606060;"> 7: </span>}</pre>
</div>
<p></p>
<p>Imaginemos então uma lista de Product carregada com diversos produtos de características diferentes:</p>
<div style="margin:15px 0;">
<pre style="font-size:small;color:white;font-family:Consolas,'Courier New',Courier,Monospace;width:100%;margin:0;"><span style="color:#606060;"> 1: </span><span style="color:DarkCyan;">List</span> products = <span style="color:#0000ff;">new</span> <span style="color:DarkCyan;">List</span>&#60;<span style="color:DarkCyan;">Product</span>&#62;();</pre>
</div>
<p></p>
<p><i>Nota: no código disponível na seção de downloads, no final do artigo, esta lista é carregada a partir de um arquivo XML que contém as informações dos produtos.</i></p>
<p>Agora podemos realizar nossas consultas na lista de produtos, que podem ser feitas de duas formas: usando os métodos de extensão (<a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" title="Extension Methods">Extension Methods</a>) disponíveis ao importar o namespace <strong>System.Linq</strong>, ou com a própria sintaxe para consultas.</p>
<p><i>Nota: Extension Methods também é um recurso introduzido no .NET Framework 3.5, que permite &#8220;adicionar&#8221; métodos em um tipo existente sem criar um tipo derivado ou modificar o tipo original.</i></p>
<h3>Usando Extension Methods</h3>
<p></p>
<p><strong>Produtos ordenados por nome</strong></p>
<div style="margin:15px 0;">
<pre style="font-size:small;color:white;font-family:Consolas,'Courier New',Courier,Monospace;width:100%;margin:0;"><span style="color:#606060;"> 1: </span><span style="color:DarkCyan;">IEnumerable</span>&#60;<span style="color:DarkCyan;">Product</span>&#62; productsByName = products.OrderBy(p =&#62;
    p.ProductName);</pre>
</div>
<p></p>
<p>Esta simples linha de código ordena os produtos por nome. O método <b>OrderBy</b> recebe uma expressão lambda (<a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" title="Lambda Expressions">Lambda Expression</a>), e retorna uma coleção ordenada conforme indicado na expressão.</p>
<p><i>Nota: Lambda Expressions são métodos traduzidos para uma instância de delegate pelo compilador, nos permitindo criar métodos on the fly e com uma sintaxe simples: &#8220;parâmetros de entrada =&#62; implementação do método&#8221;.</i></p>
<p>Também é possível imprimir o resultado da ordenação com apenas uma linha de código:</p>
<div style="margin:15px 0;">
<pre style="font-size:small;color:white;font-family:Consolas,'Courier New',Courier,Monospace;width:100%;margin:0;"><span style="color:#606060;"> 1: </span>productsByName.ToList().ForEach(p =&#62; <span style="color:DarkCyan;">Console</span>.WriteLine(
    p.ProductName));</pre>
</div>
<p></p>
<p>Ou até fazer os dois!</p>
<div style="margin:15px 0;">
<pre style="font-size:small;color:white;font-family:Consolas,'Courier New',Courier,Monospace;width:100%;margin:0;"><span style="color:#606060;"> 1: </span>products.OrderBy(p =&#62; p.ProductName)
<span style="color:#606060;"> 2: </span>        .ToList()
<span style="color:#606060;"> 3: </span>        .ForEach(p =&#62; <span style="color:DarkCyan;">Console</span>.WriteLine(p.ProductName));</pre>
</div>
<p></p>
<p>Seguem mais alguns exemplos utilizando os métodos <strong>OrderBy</strong>, <b>Reverse</b>, <b>Where</b> e <b>Sum</b>.</p>
<p><strong>Produtos ordenados por preço, decrescente</strong></p>
<div style="margin:15px 0;">
<pre style="font-size:small;color:white;font-family:Consolas,'Courier New',Courier,Monospace;width:100%;margin:0;"><span style="color:#606060;"> 1: </span><span style="color:DarkCyan;">IEnumerable</span>&#60;<span style="color:DarkCyan;">Product</span>&#62; productsByPrice = products.OrderBy(
    p =&#62; p.UnitPrice).Reverse();
<span style="color:#606060;"> 2: </span>productsByPrice.ToList().ForEach(p =&#62;
    <span style="color:DarkCyan;">Console</span>.WriteLine(String.Format(<span style="color:#606060;">{0:c} por {1}"</span>,
    p.UnitPrice, p.ProductName)));</pre>
</div>
<p></p>
<p><strong>Produtos em estoque, ordenados por unidades em estoque, decrescente</strong></p>
<div style="margin:15px 0;">
<pre style="font-size:small;color:white;font-family:Consolas,'Courier New',Courier,Monospace;width:100%;margin:0;"><span style="color:#606060;"> 1: </span><span style="color:DarkCyan;">IEnumerable</span>&#60;<span style="color:DarkCyan;">Product</span>&#62; productsInStock = products.Where(
    p =&#62; p.UnitsInStock &#62; 0).OrderBy(p =&#62; p.UnitsInStock)
    .Reverse();
<span style="color:#606060;"> 2: </span>productsInStock.ToList().ForEach(p =&#62;
    <span style="color:DarkCyan;">Console</span>.WriteLine(<span style="color:DarkCyan;">String</span>.Concat(p.UnitsInStock,
    <span style="color:#606060;">" unidades de "</span>, p.ProductName)));</pre>
</div>
<p></p>
<p><strong>Quantidade de produtos em estoque</strong></p>
<div style="margin:15px 0;">
<pre style="font-size:small;color:white;font-family:Consolas,'Courier New',Courier,Monospace;width:100%;margin:0;"><span style="color:#606060;"> 1: </span><span style="color:#0000ff;">decimal</span> productsCount = products.Sum(p =&#62; p.UnitsInStock);
<span style="color:#606060;"> 2: </span><span style="color:DarkCyan;">Console</span>.WriteLine(<span style="color:#606060;">"\nQuantidade de produtos em estoque:
    {0}"</span>, productsCount);</pre>
</div>
<p></p>
<p><strong>Valor dos produtos em estoque</strong></p>
<div style="margin:15px 0;">
<pre style="font-size:small;color:white;font-family:Consolas,'Courier New',Courier,Monospace;width:100%;margin:0;"><span style="color:#606060;"> 1: </span><span style="color:0;">decimal</span> productsValue = products.Sum(p =&#62;
    (p.UnitsInStock * p.UnitPrice));
<span style="color:#606060;"> 2: </span><span style="color:DarkCyan;">Console</span>.WriteLine(<span style="color:DarkCyan;">String</span>.Format(<span style="color:#606060;">"\nValor dos produtos em
    estoque: {0:c}"</span>, productsValue));</pre>
</div>
<p></p>
<h3>Usando a sintaxe para consultas</h3>
<p></p>
<p>Também podemos gerenciar as coleções com diversas combinações de comandos usando uma sintaxe similar à linguagem SQL. Seguem alguns exemplos:</p>
<p><strong>Produtos que começam com a letra C</strong></p>
<div style="margin:15px 0;">
<pre style="font-size:small;color:white;font-family:Consolas,'Courier New',Courier,Monospace;width:100%;margin:0;"><span style="color:#606060;"> 1: </span><span style="color:#0000ff;">var</span> productsWithC = <span style="color:#0000ff;">from</span> p <span style="color:#0000ff;">in</span> products
<span style="color:#606060;"> 2: </span>                    <span style="color:#0000ff;">where</span> p.ProductName.StartsWith(<span style="color:#606060;">"C"</span>)
<span style="color:#606060;"> 3: </span>                    <span style="color:#0000ff;">orderby</span> p.ProductName
<span style="color:#606060;"> 4: </span>                    <span style="color:#0000ff;">select</span> p;
<span style="color:#606060;"> 5: </span>productsWithC.ToList().ForEach(p =&#62;
    <span style="color:DarkCyan;">Console</span>.WriteLine(p.ProductName));</pre>
</div>
<p></p>
<p><strong>Produtos que custam mais que R$ 20,00</strong></p>
<p>É possível obter apenas as propriedades desejadas como resultado usando o comando <strong>new</strong>:</p>
<div style="margin:15px 0;">
<pre style="font-size:small;color:white;font-family:Consolas,'Courier New',Courier,Monospace;width:100%;margin:0;"><span style="color:#606060;"> 1: </span><span style="color:#0000ff;">var</span> expensiveProducts = <span style="color:#0000ff;">from</span> p <span style="color:#0000ff;">in</span> products
<span style="color:#606060;"> 2: </span>                        <span style="color:#0000ff;">where</span> p.UnitPrice &#62; 20
<span style="color:#606060;"> 3: </span>                        <span style="color:#0000ff;">orderby</span> p.UnitPrice descending
<span style="color:#606060;"> 4: </span>                        <span style="color:#0000ff;">select new</span>
<span style="color:#606060;"> 5: </span>                        {
<span style="color:#606060;"> 6: </span>                            Name = p.ProductName,
<span style="color:#606060;"> 7: </span>                            Price = p.UnitPrice
<span style="color:#606060;"> 8: </span>                        };
<span style="color:#606060;"> 9: </span>expensiveProducts.ToList().ForEach(p =&#62;
    <span style="color:DarkCyan;">Console</span>.WriteLine(<span style="color:DarkCyan;">String</span>.Format(<span style="color:#606060;">"{0:c} por {1}"</span>,
    p.Price, p.Name)));</pre>
</div>
<p></p>
<p><strong>Produto mais barato</strong></p>
<p>Também é possível retornar um único objeto com o comando <strong>Single</strong>:</p>
<div style="margin:15px 0;">
<pre style="font-size:small;color:white;font-family:Consolas,'Courier New',Courier,Monospace;width:100%;margin:0;"><span style="color:#606060;"> 1: </span><span style="color:DarkCyan;">Product</span> cheaperProduct = (<span style="color:#0000ff;">from</span> p <span style="color:#0000ff;">in</span> products
<span style="color:#606060;"> 2: </span>                          <span style="color:#0000ff;">where</span> p.UnitPrice == products
    .Min(p1 =&#62; p1.UnitPrice)
<span style="color:#606060;"> 3: </span>                          <span style="color:#0000ff;">select</span> p).Single();
<span style="color:#606060;"> 4: </span><span style="color:DarkCyan;">Console</span>.WriteLine(<span style="color:DarkCyan;">String</span>.Format(
    <span style="color:#606060;">"\nProduto mais barato:\n{0:c} por {1}"</span>,
    cheaperProduct.UnitPrice, cheaperProduct.ProductName));</pre>
</div>
<p></p>
<p>Enfim, existem diversas combinações que podem ser feitas. Daqui pra frente basta usar a imaginação&#8230; Happy coding!</p>
<p><strong>Downloads:</strong></p>
<ul>
<li><a href="http://www.freedrive.com/file/973071" title="GHR.Samples.LINQtoObjects">GHR.Samples.LINQtoObjects</a></li>
</ul>
<p></p>
<p><strong>Referências:</strong></p>
<ul>
<li>LINQ: <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx" title="LINQ">http://msdn.microsoft.com/en-us/netframework/aa904594.aspx</a></li>
<li>LINQ to Objetcs: <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" title="LINQ to Objects">http://msdn.microsoft.com/en-us/library/bb397919.aspx</a></li>
<li>Extension Methods: <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" title="Extension Methods">http://msdn.microsoft.com/en-us/library/bb383977.aspx</a></li>
<li>Lambda Expressions: <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" title="Lambda Expressions">http://msdn.microsoft.com/en-us/library/bb397687.aspx</a></li>
</ul>
<p></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Multiple List Left Join in LINQ]]></title>
<link>http://codingsense.wordpress.com/2009/06/16/multiple-list-left-join-in-linq/</link>
<pubDate>Tue, 16 Jun 2009 05:15:49 +0000</pubDate>
<dc:creator>codingsense</dc:creator>
<guid>http://codingsense.wordpress.com/2009/06/16/multiple-list-left-join-in-linq/</guid>
<description><![CDATA[We had seen in earlier posts how to left join and right join 2 lists using LINQ. But what about mult]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><font face="Verdana,Arial,Helvetica,sans-serif" size="2">We had seen in earlier posts how to <a href="http://codingsense.wordpress.com/2009/03/08/left-join-right-join-using-linq/">left join and right join</a> 2 lists using LINQ. But what about multiple list joining is it in the same way or are there any challanges in the implementation. </p>
<p>Today we shall see how we can use left join on multiple list in a single query.</p>
<p>We will take a case of 3 classes Branch ( ID, Name) , Department(ID , Name, BranchID) and employees(ID,Name,DeptID)</p>
<p><a href="http://www.box.net/shared/cetobd3qk4">Download Project &#8211; 2.9 Kb</a></p>
<p>If we would join the 3 similar tables in MSSQL then it would appear as </p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<font color="#0000FF" size="2">Select</font><font size="2"> Emp</font><font color="#808080" size="2">.</font><font size="2">Name</font><font color="#808080" size="2">,</font><font size="2"> Dept</font><font color="#808080" size="2">.</font><font size="2">Name</font><font color="#808080" size="2">,</font><font size="2"> branch</font><font color="#808080" size="2">.</font><font size="2">Name </font><font color="#0000FF" size="2">from </font><font color="#808080" size="2">((</font><font size="2">Employees </font><font color="#0000FF" size="2">as</font><font size="2"> Emp </font><font color="#808080" size="2">left <font color="#808080" size="2">join </font></font>
<font size="2">Departments </font><font color="#0000FF" size="2">as</font><font size="2"> Dept </font><font color="#0000FF" size="2">on</font><font size="2"> Dept</font><font color="#808080" size="2">.</font><font size="2">ID </font><font color="#808080" size="2">=</font><font size="2"> Emp</font><font color="#808080" size="2">.</font><font size="2">DeptID</font><font color="#808080" size="2">)left <font color="#808080" size="2">join </font></font>
<font size="2">Branches </font><font color="#0000FF" size="2">as</font><font size="2"> branch </font><font color="#0000FF" size="2">on</font><font size="2"> Dept</font><font color="#808080" size="2">.</font><font size="2">BranchID </font><font color="#808080" size="2">=</font><font size="2"> branch</font><font color="#808080" size="2">.</font><font size="2">ID</font><font color="#808080" size="2">)</font></pre>
<p>In the above query we can see that first we join Employees and department on Department ID and the output of that join is joined with branch on brachid. By following the same flow we can derive the same result using LINQ. The equivalent implementation in LINQ follows</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<font color="#0000FF" size="2">var</font><font size="2"> MultipleListJoin = </font><font color="#0000FF" size="2">from</font><font size="2"> empdept </font><font color="#0000FF" size="2">in</font>
<font size="2">                                   (</font><font color="#0000FF" size="2">from</font><font size="2"> emp </font><font color="#0000FF" size="2">in</font><font size="2"> ListOfEmployees</font>
<font color="#0000FF" size="2">join</font><font size="2"> dept </font><font color="#0000FF" size="2">in</font><font size="2"> ListOfDepartment</font>
<font color="#0000FF" size="2">on</font><font size="2"> emp.DeptID </font><font color="#0000FF" size="2">equals</font><font size="2"> dept.ID </font><font color="#0000FF" size="2">into</font><font size="2"> JoinedEmpDept</font>
<font color="#0000FF" size="2">from</font><font size="2"> dept </font><font color="#0000FF" size="2">in</font><font size="2"> JoinedEmpDept.DefaultIfEmpty()</font>
<font color="#0000FF" size="2">select <font color="#0000FF" size="2">new&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font></font>
<font size="2">{
EmployeeName = emp.Name,
BranchID = dept != </font><font color="#0000FF" size="2">null</font><font size="2"> ? dept.BranchID : 0,
DepartmentName = dept != </font><font color="#0000FF" size="2">null</font><font size="2"> ? dept.Name : </font><font color="#0000FF" size="2">null&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
<font size="2">})</font>
<font color="#0000FF" size="2">join</font><font size="2"> branch </font><font color="#0000FF" size="2">in</font><font size="2"> ListOfBranch </font><font color="#0000FF" size="2">on</font>
<font size="2">                               empdept.BranchID </font><font color="#0000FF" size="2">equals</font><font size="2"> branch.ID </font><font color="#0000FF" size="2">into</font><font size="2"> JoinedwithBranch</font>
<font color="#0000FF" size="2">from</font><font size="2"> branch </font><font color="#0000FF" size="2">in</font><font size="2"> JoinedwithBranch.DefaultIfEmpty()</font>
<font color="#0000FF" size="2">select <font color="#0000FF" size="2">new&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font></font>
<font size="2">{
EmpName = empdept.EmployeeName,
DeptName = empdept.DepartmentName,
BranchName = branch != </font><font color="#0000FF" size="2">null</font><font size="2"> ? branch.Name : </font><font color="#0000FF" size="2">string</font><font size="2">.Empty
};</font></pre>
<p>In the above first we derive the intermediate ouput by joining Employee and Department</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<font color="#0000FF" size="2">from</font><font size="2"> emp </font><font color="#0000FF" size="2">in</font><font size="2"> ListOfEmployees</font>
<font color="#0000FF" size="2">join</font><font size="2"> dept </font><font color="#0000FF" size="2">in</font><font size="2"> ListOfDepartment</font>
<font color="#0000FF" size="2">on</font><font size="2"> emp.DeptID </font><font color="#0000FF" size="2">equals</font><font size="2"> dept.ID </font><font color="#0000FF" size="2">into</font><font size="2"> JoinedEmpDept</font>
<font color="#0000FF" size="2">from</font><font size="2"> dept </font><font color="#0000FF" size="2">in</font><font size="2"> JoinedEmpDept.DefaultIfEmpty()</font>
<font color="#0000FF" size="2">select <font color="#0000FF" size="2">new&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font></font>
<font size="2">{
EmployeeName = emp.Name,
BranchID = dept != </font><font color="#0000FF" size="2">null</font><font size="2"> ? dept.BranchID : 0,
DepartmentName = dept != </font><font color="#0000FF" size="2">null</font><font size="2"> ? dept.Name : </font><font color="#0000FF" size="2">null}</font></pre>
<p>and then use the intemediate output and join it with Branches to get the final result.</p>
<p><a href="http://codingsense.wordpress.com/2009/06/16/multiple-list-left-join-in-linq/multiplelistleftjoin/" rel="attachment wp-att-309"><img src="http://codingsense.wordpress.com/files/2009/06/multiplelistleftjoin.jpg" alt="MultipleListLeftJoin" title="MultipleListLeftJoin" width="500" height="126" class="alignnone size-full wp-image-309" /></a></p>
<p>Hope you have got how to join multiple lists, for <a href="http://codingsense.wordpress.com/2009/03/08/left-join-right-join-using-linq/">Right join</a> follow the above implementataion in the reverse way.</p>
<p>Happy Learning <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
</font></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[LINQ to YAML]]></title>
<link>http://alexreg.wordpress.com/2009/05/14/linq-to-yaml/</link>
<pubDate>Wed, 13 May 2009 23:53:48 +0000</pubDate>
<dc:creator>Noldorin</dc:creator>
<guid>http://alexreg.wordpress.com/2009/05/14/linq-to-yaml/</guid>
<description><![CDATA[LINQ to XML is one of the many technologies introduced with the .NET Framework 3.5, and one that is ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><a href="http://msdn.microsoft.com/en-us/library/bb387098.aspx">LINQ to XML</a> is one of the many technologies introduced with the .NET Framework 3.5, and one that is certainly a step forward in terms of usability. It allows querying in both the functional style (using LINQ and lambda expressions) and the more traditional imperative one, meaning that it&#8217;s a great tool for concisely working with XML data in any sort of application, and undoubtedly a significant improvement over the old XML DOM that resides in the <em>System.Xml</em> namespace.</p>
<p>In the spirit of LINQ, and with the advent of <a href="http://www.yaml.org/">YAML</a>, I recntly decided it was about time that this new &#8220;markup language&#8221; were integrated with LINQ. Surprisingly, there does not already exist anything akin to LINQ to YAML out there (though there are a couple of fairly usable implementations of a YAML reader/writer for .NET). This seemed to me like a good chance to potentially create something that might be used by more than the odd .NET developer or two. My plans are to implement a LINQ to YAML provider either from scratch or on top of one of the existing YAML libraries. (Which option I choose will depend on the state of the existing projects, which I haven&#8217;t yet investigated properly. I am however suspecting that it might be worthwhile writing my own, since it would a) teach me all the intricacies of YAML, and b) allow me to support the latest version [1.2], which the existing libraries do not.)</p>
<p>Before I launch into an overview of my intended implementation, here is a little bit about YAML itself, for those who aren&#8217;t already familiar with it. Although technically YAML isn&#8217;t a markup language (after all, the recursive acronym stands for <em>YAML Ain&#8217;t Markup Language</em>) &#8211; it is rather a serialisation format &#8211; it does essentially fulfill the the role that XML  traditionally has, in a variety of common situations. I&#8217;m not going to try to sell the format to you right now, but it should suffice to say that you wouldn&#8217;t have reached this far in the post if you weren&#8217;t already at least intrigued! Without doubt, the format is actively gaining popularity because of it&#8217;s ultra-lightweight syntax and suitability for hand editing, perhaps the two points that summarise its advantages over XML.</p>
<p>Anyway, here&#8217;s a short example of a YAML document (taken straight from <a href="http://en.wikipedia.org/wiki/Yaml#Sample_document">the Wikipedia page</a>), so you can see precisely how pleasant it is to work with (at least for humans).</p>
<p>&#8212;</p>
<pre>receipt:     Oz-Ware Purchase Invoice
date:        2007-08-06
customer:
    given:   Dorothy
    family:  Gale

items:
    - part_no:   A4786
      descrip:   Water Bucket (Filled)
      price:     1.47
      quantity:  4

    - part_no:   E1628
      descrip:   High Heeled "Ruby" Slippers
      price:     100.27
      quantity:  1

bill-to:  &#38;id001
    street: &#124;
            123 Tornado Alley
            Suite 16
    city:   East Westville
    state:  KS

ship-to:  *id001

specialDelivery:  &#62;
    Follow the Yellow Brick
    Road to the Emerald City.
    Pay no attention to the
    man behind the curtain.
...</pre>
<p>Of course, the great thing about YAML, which is demonstrated clearly by this example, is that you don&#8217;t have to have any real knowledge about YAML to understand exactly and immediately what the data represents, and as a bonus it doesn&#8217;t hurt your eyes to stare at for too long! Even the referencing syntax should be fairly self evident. (<em>&#38;id00</em> and<em> *id001</em> would surely be nothing new to C programmers.)</p>
<p>The semantics as well as the syntax of YAML obviously differ to those of XML greatly, although there is almost always some sort of correspondence between the features and possibilities that the two formats offer. The only notable missing feature when contrasted to XML is attributes, yet their usefulness is questionable anyway.</p>
<p>Right, so now I ought to explain a bit about how I actually plan to design this library. The basic framework will be virtually equivalent to that of LINQ to XML. In other words, the hierarchy will be largely based around an abstract <em>YamlObject</em> (<em>YObject</em>?) class, and will look very much like the one contained within <em>System.Xml.Linq</em>.</p>
<p><a href="http://i.msdn.microsoft.com/Bb308960.xlinqoverview01(en-us,MSDN.10).jpg"><img style="width:100%;" src="http://i.msdn.microsoft.com/Bb308960.xlinqoverview01(en-us,MSDN.10).jpg" alt="Diagram of LINQ to XML class hierarchy" /></a><em>LINQ to XML class hierarchy</em></p>
<p>Though LINQ to YAML must of course accomodate for the unique nature of the format, I would initially aim for minimal difference and only significantly adjust the hierarchy when it is found to be necessary. Classes such as <em>XCData</em> and <em>XDocumentType</em> would not apply at all to YAML, yet there would need to be a place for a <em>YReference</em> or such somewhere in the hierarchy. The referencing aspect of YAML will likely prove to be one of the more interesting challenges; while YAML&#8217;s lists, maps (dictionaries), and combinations thereof would seem relatively straightforward with regards to emulation of the LINQ to XML design, references would introduce a substantially novel concept. Some sort of implementation of lazy evaluation followed by concrete referencing should be able to solve the problem, but there&#8217;s no way to predict how well this might work in practice at this moment.</p>
<p>What I realised only after deciding to create a LINQ to YAML library is that among LINQ providers, LINQ to XML is somewhat special in that the LINQ aspect of it is built <em>on top of</em> <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx">LINQ to Objects</a> (i.e. LINQ using <em>IEnumerable&#60;T&#62;</em> objects), with only a relatively small number of extension methods specific to LINQ to XML. Indeed, most LINQ providers (LINQ to Objects and LINQ to SQL among others) require you to implement the <em>IQueryable</em> and <em>IQueryProvider</em> interfaces to provide complex logic for interpreting and returning the results of expressions, as well as evaluating complex expression trees.  All this means that I can pretty much just design a DOM  to a certain style (i.e. one suited to functional code, like LINQ to XML), and let LINQ to Objects to everything else for me.</p>
<p>As I can&#8217;t think of anything more worth mentioning about my project at this time, I shall leave any more specific and complex details to a future post. Still, do by all means feel free to query me about my plans &#8211; I would be glad to answer any questions, and even gladder to receive some suggestions as how you think I might design LINQ to YAML, or simply a nod that you might find this useful at some point. I don&#8217;t anticipate this project to be a very long one, though I must say that both my work and free-time schedule are likely to be fairly messed up for the next month or two, therefore I&#8217;m not going to promise when I&#8217;ll get around to my initial release. Whenever it so happens, I will duly post the link to the project page on Launchpad (or wherever I decide to host it).</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[LINQ to Objects: Forced Result Set Evaluation]]></title>
<link>http://platinumdogs.wordpress.com/2009/05/06/linq-to-objects-forced-result-set-evaluation/</link>
<pubDate>Wed, 06 May 2009 13:13:25 +0000</pubDate>
<dc:creator>Phil Harding</dc:creator>
<guid>http://platinumdogs.wordpress.com/2009/05/06/linq-to-objects-forced-result-set-evaluation/</guid>
<description><![CDATA[Usually when using LINQ for querying object collections, evaluation or realisation of the result set]]></description>
<content:encoded><![CDATA[Usually when using LINQ for querying object collections, evaluation or realisation of the result set]]></content:encoded>
</item>
<item>
<title><![CDATA[Left Join, Right Join Using LINQ]]></title>
<link>http://codingsense.wordpress.com/2009/03/08/left-join-right-join-using-linq/</link>
<pubDate>Sun, 08 Mar 2009 12:02:48 +0000</pubDate>
<dc:creator>codingsense</dc:creator>
<guid>http://codingsense.wordpress.com/2009/03/08/left-join-right-join-using-linq/</guid>
<description><![CDATA[Hi Friends, In this post let us see how we can handle Left Join and Right Join when using LINQ. Ther]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;">Hi Friends,</span></p>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:x-small;"> </span></p>
<p>In this post let us see how we can handle Left Join and Right Join when using LINQ. There are no keywords defined in C#, we have to use DefaultIfEmpty() function to get the desired result.<br />
Let us see how we can achieve it.</p>
<p>To make you understand better I use a Employee -&#62; Department realation to explain.</p>
<p>First we shall create two classes namely Employee and Department</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:x-small;">class </span><span style="color:#2b91af;font-size:x-small;">Employee    </span>
<span style="font-size:x-small;">{</span>
<span style="color:#0000ff;font-size:x-small;">public <span style="color:#0000ff;font-size:x-small;">string</span></span><span style="font-size:x-small;"> Name { </span><span style="color:#0000ff;font-size:x-small;">get</span><span style="font-size:x-small;">; </span><span style="color:#0000ff;font-size:x-small;">set</span><span style="font-size:x-small;">; }</span>
<span style="color:#0000ff;font-size:x-small;">public <span style="color:#0000ff;font-size:x-small;">int</span></span><span style="font-size:x-small;"> ID { </span><span style="color:#0000ff;font-size:x-small;">get</span><span style="font-size:x-small;">; </span><span style="color:#0000ff;font-size:x-small;">set</span><span style="font-size:x-small;">; }</span>
<span style="color:#0000ff;font-size:x-small;">public <span style="color:#0000ff;font-size:x-small;">int</span></span><span style="font-size:x-small;"> DeptID { </span><span style="color:#0000ff;font-size:x-small;">get</span><span style="font-size:x-small;">; </span><span style="color:#0000ff;font-size:x-small;">set</span><span style="font-size:x-small;">; }
}</span>

<span style="color:#0000ff;font-size:x-small;">class </span><span style="color:#2b91af;font-size:x-small;">Department    </span>
<span style="font-size:x-small;">{</span>
<span style="color:#0000ff;font-size:x-small;">public <span style="color:#0000ff;font-size:x-small;">int</span></span><span style="font-size:x-small;"> ID { </span><span style="color:#0000ff;font-size:x-small;">get</span><span style="font-size:x-small;">; </span><span style="color:#0000ff;font-size:x-small;">set</span><span style="font-size:x-small;">; }</span>
<span style="color:#0000ff;font-size:x-small;">public <span style="color:#0000ff;font-size:x-small;">string</span></span><span style="font-size:x-small;"> Name { </span><span style="color:#0000ff;font-size:x-small;">get</span><span style="font-size:x-small;">; </span><span style="color:#0000ff;font-size:x-small;">set</span><span style="font-size:x-small;">; }
}</span></pre>
<p>Lets create some objects of both the classes and fill some dummy data in it.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;"> emp1 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">() { ID = 1, Name = </span><span style="color:#a31515;font-size:x-small;">"A"</span><span style="font-size:x-small;">, DeptID = 1};</span>
<span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;"> emp2 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">() { ID = 2, Name = </span><span style="color:#a31515;font-size:x-small;">"B"</span><span style="font-size:x-small;">, DeptID = 1};</span>
<span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;"> emp3 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">() { ID = 3, Name = </span><span style="color:#a31515;font-size:x-small;">"C"</span><span style="font-size:x-small;">, DeptID = 1 };</span>
<span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;"> emp4 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">() { ID = 4, Name = </span><span style="color:#a31515;font-size:x-small;">"D"</span><span style="font-size:x-small;">, DeptID = 2 };</span>
<span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;"> emp5 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">() { ID = 5, Name = </span><span style="color:#a31515;font-size:x-small;">"E"</span><span style="font-size:x-small;">, DeptID = 2 };</span>
<span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;"> emp6 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">() { ID = 6, Name = </span><span style="color:#a31515;font-size:x-small;">"F"</span><span style="font-size:x-small;">, DeptID = 2 };</span>
<span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;"> emp7 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">() { ID = 7, Name = </span><span style="color:#a31515;font-size:x-small;">"G"</span><span style="font-size:x-small;">, DeptID = 6 };</span>
<span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;"> emp8 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">() { ID = 8, Name = </span><span style="color:#a31515;font-size:x-small;">"H"</span><span style="font-size:x-small;">, DeptID = 3 };</span>
<span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;"> emp9 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">() { ID = 9, Name = </span><span style="color:#a31515;font-size:x-small;">"I"</span><span style="font-size:x-small;">, DeptID = 3 };</span>
<span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;"> emp10 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">() { ID = 10, Name = </span><span style="color:#a31515;font-size:x-small;">"J"</span><span style="font-size:x-small;">, DeptID = 7};</span>
<span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;"> emp11 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">() { ID = 11, Name = </span><span style="color:#a31515;font-size:x-small;">"K"</span><span style="font-size:x-small;">, DeptID = 7};</span>
<span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;"> emp12 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">() { ID = 12, Name = </span><span style="color:#a31515;font-size:x-small;">"L"</span><span style="font-size:x-small;">, DeptID = 5};</span>

<span style="color:#2b91af;font-size:x-small;">Department</span><span style="font-size:x-small;"> Dept1 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Department</span><span style="font-size:x-small;">() { ID = 1, Name = </span><span style="color:#a31515;font-size:x-small;">"Development"</span><span style="font-size:x-small;">};</span>
<span style="color:#2b91af;font-size:x-small;">Department</span><span style="font-size:x-small;"> Dept2 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Department</span><span style="font-size:x-small;">() { ID = 2, Name = </span><span style="color:#a31515;font-size:x-small;">"Testing"</span><span style="font-size:x-small;">};</span>
<span style="color:#2b91af;font-size:x-small;">Department</span><span style="font-size:x-small;"> Dept3 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Department</span><span style="font-size:x-small;">() { ID = 3, Name = </span><span style="color:#a31515;font-size:x-small;">"Marketing"</span><span style="font-size:x-small;">};</span>
<span style="color:#2b91af;font-size:x-small;">Department</span><span style="font-size:x-small;"> Dept4 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Department</span><span style="font-size:x-small;">() { ID = 4, Name = </span><span style="color:#a31515;font-size:x-small;">"Support"</span><span style="font-size:x-small;">};</span>

<span style="color:#2b91af;font-size:x-small;">List</span><span style="font-size:x-small;">&#60;</span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">&#62; ListOfEmployees = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">List</span><span style="font-size:x-small;">&#60;</span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">&#62;();
ListOfEmployees.AddRange((</span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Employee</span><span style="font-size:x-small;">[] { emp1, emp2, emp3, emp4, emp5, emp6, emp7,
emp8, emp9, emp10, emp11, emp12 }));</span>

<span style="color:#2b91af;font-size:x-small;">List</span><span style="font-size:x-small;">&#60;</span><span style="color:#2b91af;font-size:x-small;">Department</span><span style="font-size:x-small;">&#62; ListOfDepartment = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">List</span><span style="font-size:x-small;">&#60;</span><span style="color:#2b91af;font-size:x-small;">Department</span><span style="font-size:x-small;">&#62;();
ListOfDepartment.AddRange( </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Department</span><span style="font-size:x-small;">[]{ Dept1,Dept2,Dept3,Dept4});</span></pre>
<p>So we finish loading the objects into ListOfEmployees and ListOfDepartments, using this lists we shall see how we can join them to get the results.<br />
First let us see what would be the query in SQL if we had the same structure in our tables.<br />
For Left join and right join we would have used the query</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#008000;font-size:x-small;">--Left Join in SQL</span>
<span style="color:#0000ff;font-size:x-small;">select</span><span style="font-size:x-small;"> Emp</span><span style="color:#808080;font-size:x-small;">.</span><span style="font-size:x-small;">Name</span><span style="color:#808080;font-size:x-small;">,</span><span style="font-size:x-small;"> Dept</span><span style="color:#808080;font-size:x-small;">.</span><span style="font-size:x-small;">Name </span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> Employee Emp </span><span style="color:#808080;font-size:x-small;">left <span style="color:#808080;font-size:x-small;">join</span></span><span style="font-size:x-small;"> Department Dept </span><span style="color:#0000ff;font-size:x-small;">on</span>
<span style="font-size:x-small;">Emp</span><span style="color:#808080;font-size:x-small;">.</span><span style="font-size:x-small;">DeptID </span><span style="color:#808080;font-size:x-small;">=</span><span style="font-size:x-small;"> Dept</span><span style="color:#808080;font-size:x-small;">.</span><span style="font-size:x-small;">ID</span>

<span style="color:#008000;font-size:x-small;">--Right Join In SQL</span>
<span style="color:#0000ff;font-size:x-small;">select</span><span style="font-size:x-small;"> Emp</span><span style="color:#808080;font-size:x-small;">.</span><span style="font-size:x-small;">Name</span><span style="color:#808080;font-size:x-small;">,</span><span style="font-size:x-small;"> Dept</span><span style="color:#808080;font-size:x-small;">.</span><span style="font-size:x-small;">Name </span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> Employee Emp </span><span style="color:#808080;font-size:x-small;">right <span style="color:#808080;font-size:x-small;">join</span></span><span style="font-size:x-small;"> Department Dept </span><span style="color:#0000ff;font-size:x-small;">on</span>
<span style="font-size:x-small;">Emp</span><span style="color:#808080;font-size:x-small;">.</span><span style="font-size:x-small;">DeptID </span><span style="color:#808080;font-size:x-small;">=</span><span style="font-size:x-small;"> Dept</span><span style="color:#808080;font-size:x-small;">.</span><span style="font-size:x-small;">ID</span></pre>
<p>Using LINQ, <strong>Left Join</strong> can be acheived as follows</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:x-small;">var</span><span style="font-size:x-small;"> LeftJoin = </span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> emp </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfEmployees</span>
<span style="color:#0000ff;font-size:x-small;">join</span><span style="font-size:x-small;"> dept </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfDepartment</span>
<span style="color:#0000ff;font-size:x-small;">on</span><span style="font-size:x-small;"> emp.DeptID </span><span style="color:#0000ff;font-size:x-small;">equals</span><span style="font-size:x-small;"> dept.ID </span><span style="color:#0000ff;font-size:x-small;">into</span><span style="font-size:x-small;"> JoinedEmpDept </span>
<span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> dept </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> JoinedEmpDept.DefaultIfEmpty()</span>
<span style="color:#0000ff;font-size:x-small;">select <span style="color:#0000ff;font-size:x-small;">new                          </span></span>
<span style="font-size:x-small;">{
EmployeeName = emp.Name,
DepartmentName = dept != </span><span style="color:#0000ff;font-size:x-small;">null</span><span style="font-size:x-small;"> ? dept.Name : </span><span style="color:#0000ff;font-size:x-small;">null                          </span>
<span style="font-size:x-small;">};</span></pre>
<p>And for <strong>Right Join</strong> there is no pretty difference, we just need to reverse the joining in first 2 lines. Here it follows</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:x-small;">var</span><span style="font-size:x-small;"> RightJoin = </span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> dept </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfDepartment</span>
<span style="color:#0000ff;font-size:x-small;">join</span><span style="font-size:x-small;"> employee </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfEmployees</span>
<span style="color:#0000ff;font-size:x-small;">on</span><span style="font-size:x-small;"> dept.ID </span><span style="color:#0000ff;font-size:x-small;">equals</span><span style="font-size:x-small;"> employee.DeptID </span><span style="color:#0000ff;font-size:x-small;">into</span><span style="font-size:x-small;"> joinDeptEmp</span>
<span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> employee </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> joinDeptEmp.DefaultIfEmpty()</span>
<span style="color:#0000ff;font-size:x-small;">select <span style="color:#0000ff;font-size:x-small;">new                            </span></span>
<span style="font-size:x-small;">{
EmployeeName = employee != </span><span style="color:#0000ff;font-size:x-small;">null</span><span style="font-size:x-small;"> ? employee.Name : </span><span style="color:#0000ff;font-size:x-small;">null</span><span style="font-size:x-small;">,
DepartmentName = dept.Name
};</span></pre>
<p>Do you believe it will work ??? lets check it by displaying,</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#2b91af;font-size:x-small;">Console</span><span style="font-size:x-small;">.WriteLine(</span><span style="color:#0000ff;font-size:x-small;">string</span><span style="font-size:x-small;">.Join(</span><span style="color:#a31515;font-size:x-small;">"n"</span><span style="font-size:x-small;"> , LeftJoin.Select(emp =&#62; </span><span style="color:#a31515;font-size:x-small;">" Employee Name = "</span><span style="font-size:x-small;"> +
emp.EmployeeName + </span><span style="color:#a31515;font-size:x-small;">", Department Name = "</span><span style="font-size:x-small;"> + emp.DepartmentName).ToArray&#60;</span><span style="color:#0000ff;font-size:x-small;">string</span><span style="font-size:x-small;">&#62;()));</span>

<span style="color:#2b91af;font-size:x-small;">Console</span><span style="font-size:x-small;">.WriteLine(</span><span style="color:#0000ff;font-size:x-small;">string</span><span style="font-size:x-small;">.Join(</span><span style="color:#a31515;font-size:x-small;">"n"</span><span style="font-size:x-small;">, RightJoin.Select(emp =&#62; </span><span style="color:#a31515;font-size:x-small;">" Employee Name = "</span><span style="font-size:x-small;"> +
emp.EmployeeName + </span><span style="color:#a31515;font-size:x-small;">", Department Name = "</span><span style="font-size:x-small;"> + emp.DepartmentName).ToArray&#60;</span><span style="color:#0000ff;font-size:x-small;">string</span><span style="font-size:x-small;">&#62;()));</span></pre>
<p><span style="text-decoration:underline;"></p>
<div id="attachment_267" class="wp-caption alignnone" style="width: 310px"><a rel="attachment wp-att-267" href="http://codingsense.wordpress.com/2009/03/08/left-join-right-join-using-linq/leftjoinlinq1/"><img class="size-medium wp-image-267" title="leftjoinlinq1" src="http://codingsense.wordpress.com/files/2009/03/leftjoinlinq1.jpg?w=300" alt="Output of Left Join in LINQ" width="300" height="130" /></a><p class="wp-caption-text">Output of Left Join in LINQ</p></div>
<div id="attachment_268" class="wp-caption alignnone" style="width: 310px"><a rel="attachment wp-att-268" href="http://codingsense.wordpress.com/2009/03/08/left-join-right-join-using-linq/rightjoinlinq2/"><img class="size-medium wp-image-268" title="rightjoinlinq2" src="http://codingsense.wordpress.com/files/2009/03/rightjoinlinq2.jpg?w=300" alt="Output of Right Join in LINQ" width="300" height="103" /></a><p class="wp-caption-text">Output of Right Join in LINQ</p></div>
<p></span></p>
<p>Great, it has worked. So friends in this way you can handle Left and Right Joins in LINQ, hope you understood the sample.</p>
<p>Next -&#62; <a href="http://codingsense.wordpress.com/2009/06/16/multiple-list-left-join-in-linq/">Multiple List Left Join</a></p>
<p>Happy Learning <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Join in LINQ]]></title>
<link>http://codingsense.wordpress.com/2009/03/08/join-in-linq/</link>
<pubDate>Sun, 08 Mar 2009 06:13:05 +0000</pubDate>
<dc:creator>codingsense</dc:creator>
<guid>http://codingsense.wordpress.com/2009/03/08/join-in-linq/</guid>
<description><![CDATA[Hi Friends, In this post lets us try to understand how Join is handled using LINQ. We will take a sa]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><font face="Verdana,Arial,Helvetica,sans-serif" size="2">Hi Friends,</p>
<p>In this post lets us try to understand how Join is handled using LINQ. We will take a sample that would make us clear what we are trying to acheive.</p>
<p>Consider a department that handles a type of category of items, it can be active or not. The class of the department will look like </p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<font color="#0000FF" size="2">public <font color="#0000FF" size="2">class </font></font><font color="#2B91AF" size="2">Dept&#160;&#160;&#160;&#160;</font>
<font size="2">{</font>
<font color="#0000FF" size="2">public <font color="#0000FF" size="2">int</font></font><font size="2"> ID { </font><font color="#0000FF" size="2">get</font><font size="2">; </font><font color="#0000FF" size="2">set</font><font size="2">; }</font>
<font color="#0000FF" size="2">public <font color="#0000FF" size="2">int</font></font><font size="2"> CategoryID { </font><font color="#0000FF" size="2">get</font><font size="2">; </font><font color="#0000FF" size="2">set</font><font size="2">; }</font>
<font color="#0000FF" size="2">public <font color="#0000FF" size="2">bool</font></font><font size="2"> IsActive { </font><font color="#0000FF" size="2">get</font><font size="2">; </font><font color="#0000FF" size="2">set</font><font size="2">; }
}</font>
</pre>
<p>After creating the department class lets move to create the Items class</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<font color="#0000FF" size="2">public <font color="#0000FF" size="2">class </font></font><font color="#2B91AF" size="2">Item&#160;&#160;&#160;&#160;</font>
<font size="2">{</font>
<font color="#0000FF" size="2">public <font color="#0000FF" size="2">int</font></font><font size="2"> ID { </font><font color="#0000FF" size="2">get</font><font size="2">; </font><font color="#0000FF" size="2">set</font><font size="2">; }</font>
<font color="#0000FF" size="2">public <font color="#0000FF" size="2">int</font></font><font size="2"> DeptID { </font><font color="#0000FF" size="2">get</font><font size="2">; </font><font color="#0000FF" size="2">set</font><font size="2">; }</font>
<font color="#0000FF" size="2">public <font color="#0000FF" size="2">int</font></font><font size="2"> CategoryID { </font><font color="#0000FF" size="2">get</font><font size="2">; </font><font color="#0000FF" size="2">set</font><font size="2">; }</font>
<font color="#0000FF" size="2">public <font color="#0000FF" size="2">bool</font></font><font size="2"> IsActive { </font><font color="#0000FF" size="2">get</font><font size="2">; </font><font color="#0000FF" size="2">set</font><font size="2">; }</font>
<font color="#0000FF" size="2">public <font color="#0000FF" size="2">int</font></font><font size="2"> Quantity { </font><font color="#0000FF" size="2">get</font><font size="2">; </font><font color="#0000FF" size="2">set</font><font size="2">; }
}</font>
</pre>
<p>First let us create a list of Department and Items </p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<font color="#008000" size="2">//Create Items&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
<font color="#2B91AF" size="2">Item</font><font size="2"> itm1 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Item</font><font size="2">() { ID = 1, DeptID = 1, CategoryID = 1, IsActive = </font><font color="#0000FF" size="2">true</font><font size="2">, Quantity = 30 };</font>
<font color="#2B91AF" size="2">Item</font><font size="2"> itm2 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Item</font><font size="2">() { ID = 2, DeptID = 1, CategoryID = 1, IsActive = </font><font color="#0000FF" size="2">false</font><font size="2">, Quantity = 5 };</font>
<font color="#2B91AF" size="2">Item</font><font size="2"> itm3 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Item</font><font size="2">() { ID = 3, DeptID = 1, CategoryID = 2, IsActive = </font><font color="#0000FF" size="2">true</font><font size="2">, Quantity = 2 };</font>
<font color="#2B91AF" size="2">Item</font><font size="2"> itm4 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Item</font><font size="2">() { ID = 4, DeptID = 1, CategoryID = 2, IsActive = </font><font color="#0000FF" size="2">false</font><font size="2">, Quantity = 0 };</font>

<font color="#2B91AF" size="2">Item</font><font size="2"> itm5 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Item</font><font size="2">() { ID = 5, DeptID = 2, CategoryID = 1, IsActive = </font><font color="#0000FF" size="2">true</font><font size="2">, Quantity = 0 };</font>
<font color="#2B91AF" size="2">Item</font><font size="2"> itm6 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Item</font><font size="2">() { ID = 6, DeptID = 2, CategoryID = 1, IsActive = </font><font color="#0000FF" size="2">false</font><font size="2">, Quantity = 0 };</font>
<font color="#2B91AF" size="2">Item</font><font size="2"> itm7 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Item</font><font size="2">() { ID = 7, DeptID = 2, CategoryID = 2, IsActive = </font><font color="#0000FF" size="2">true</font><font size="2">, Quantity = 20 };</font>
<font color="#2B91AF" size="2">Item</font><font size="2"> itm8 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Item</font><font size="2">() { ID = 8, DeptID = 2, CategoryID = 2, IsActive = </font><font color="#0000FF" size="2">false</font><font size="2">, Quantity = 5 };</font>

<font color="#2B91AF" size="2">Item</font><font size="2">[] ListOfItems = { itm1, itm2, itm3, itm4, itm5, itm6, itm7, itm8 };</font>

<font color="#008000" size="2">//Create Departments&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
<font color="#2B91AF" size="2">Dept</font><font size="2"> dept1 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Dept</font><font size="2">() { ID = 1, CategoryID = 1, IsActive = </font><font color="#0000FF" size="2">true </font><font size="2">};</font>
<font color="#2B91AF" size="2">Dept</font><font size="2"> dept2 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Dept</font><font size="2">() { ID = 1, CategoryID = 1, IsActive = </font><font color="#0000FF" size="2">false</font><font size="2">};</font>
<font color="#2B91AF" size="2">Dept</font><font size="2"> dept3 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Dept</font><font size="2">() { ID = 1, CategoryID = 2, IsActive = </font><font color="#0000FF" size="2">true </font><font size="2">};</font>
<font color="#2B91AF" size="2">Dept</font><font size="2"> dept4 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Dept</font><font size="2">() { ID = 1, CategoryID = 2, IsActive = </font><font color="#0000FF" size="2">false </font><font size="2">};</font>

<font color="#2B91AF" size="2">Dept</font><font size="2"> dept5 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Dept</font><font size="2">() { ID = 2, CategoryID = 1, IsActive = </font><font color="#0000FF" size="2">true </font><font size="2">};</font>
<font color="#2B91AF" size="2">Dept</font><font size="2"> dept6 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Dept</font><font size="2">() { ID = 2, CategoryID = 1, IsActive = </font><font color="#0000FF" size="2">false </font><font size="2">};</font>
<font color="#2B91AF" size="2">Dept</font><font size="2"> dept7 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Dept</font><font size="2">() { ID = 2, CategoryID = 2, IsActive = </font><font color="#0000FF" size="2">true </font><font size="2">};</font>
<font color="#2B91AF" size="2">Dept</font><font size="2"> dept8 = </font><font color="#0000FF" size="2">new </font><font color="#2B91AF" size="2">Dept</font><font size="2">() { ID = 2, CategoryID = 2, IsActive = </font><font color="#0000FF" size="2">false </font><font size="2">};</font>

<font color="#2B91AF" size="2">Dept</font><font size="2">[] ListOfDept = { dept1, dept2, dept3, dept4,dept5,dept6,dept7,dept8 };</font>
</pre>
<p>Lets move furthur to check how these two classes can be joined to derive the result</p>
<p><b>Join single column-&#62; No of items in Department 1:</b><br />
We will join the two classes on department id and get the the sum of items present in department 1.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<font color="#008000" size="2">//Single Join&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
<font color="#008000" size="2">//List No of items in Dept 1&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font></font>
<font color="#0000FF" size="2">int</font><font size="2"> NoOfItems = (</font><font color="#0000FF" size="2">from</font><font size="2"> dept </font><font color="#0000FF" size="2">in</font><font size="2"> ListOfDept</font>
<font color="#0000FF" size="2">join</font><font size="2"> item </font><font color="#0000FF" size="2">in</font><font size="2"> ListOfItems</font>
<font color="#0000FF" size="2">on</font><font size="2"> dept.ID </font><font color="#0000FF" size="2">equals</font><font size="2"> item.DeptID </font>
<font color="#0000FF" size="2">where</font><font size="2"> dept.ID == 1</font>
<font color="#0000FF" size="2">select <font color="#0000FF" size="2">new </font></font><font size="2">{ item.ID, item.Quantity }).Distinct().Sum(itm =&#62; itm.Quantity);</font>
</pre>
<p>In the above code I am filtering the same records that are returned due to equijoin.</p>
<p><b>Join multiple columns -&#62; Get the item * and dept * for the following criteria</b><br />
Lets join the dept and item objects on the following criteria</p>
<p><div id="attachment_251" class="wp-caption alignnone" style="width: 310px"><a href="http://codingsense.wordpress.com/2009/03/08/join-in-linq/joinlinq/" rel="attachment wp-att-251"><img src="http://codingsense.wordpress.com/files/2009/03/joinlinq.jpg?w=300" alt="Relation between Department and Items" title="joinlinq" width="300" height="140" class="size-medium wp-image-251" /></a><p class="wp-caption-text">Relation between Department and Items</p></div><br />
and add a extra condition for Item.Quantity &#62; 0 and retrive the values</p>
<p>First let us see how the above result are acheived <b>without using join</b> to make you clearly understand the difference between using and not using the join</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<font color="#0000FF" size="2">var</font><font size="2"> WithoutJoin = </font><font color="#0000FF" size="2">from</font><font size="2"> dept </font><font color="#0000FF" size="2">in</font><font size="2"> ListOfDept</font>
<font color="#0000FF" size="2">from</font><font size="2"> item </font><font color="#0000FF" size="2">in</font><font size="2"> ListOfItems</font>
<font color="#0000FF" size="2">where</font><font size="2"> dept.ID == item.DeptID &#38;&#38;
dept.CategoryID == item.CategoryID &#38;&#38;
dept.IsActive == item.IsActive &#38;&#38;
item.IsActive == </font><font color="#0000FF" size="2">true</font><font size="2"> &#38;&#38;
item.Quantity &#62; 0</font>
<font color="#0000FF" size="2">select <font color="#0000FF" size="2">new </font></font><font size="2">{ item, dept };</font>
</pre>
<p>Now lets retrive the same values <b>using join</b></p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<font color="#0000FF" size="2">var</font><font size="2"> WithJoin = </font><font color="#0000FF" size="2">from</font><font size="2"> dept </font><font color="#0000FF" size="2">in</font><font size="2"> ListOfDept</font>
<font color="#0000FF" size="2">join</font><font size="2"> item </font><font color="#0000FF" size="2">in</font><font size="2"> ListOfItems</font>
<font color="#0000FF" size="2">on <font color="#0000FF" size="2">new </font></font><font size="2">{ deptid = dept.ID, catID = dept.CategoryID , isActive = dept.IsActive } </font><font color="#0000FF" size="2">equals
<font color="#0000FF" size="2">new </font></font><font size="2">{ deptid = item.DeptID, catID = item.CategoryID, isActive = item.IsActive }</font>
<font color="#0000FF" size="2">where</font><font size="2"> item.IsActive == </font><font color="#0000FF" size="2">true</font><font size="2"> &#38;&#38; item.Quantity &#62; 0</font>
<font color="#0000FF" size="2">select <font color="#0000FF" size="2">new </font></font><font size="2">{ item, dept };</font>
</pre>
<p>Hope you have understood how to join single column and join multiple columns using LINQ. If you need any clarification please feel free.</p>
<p>Happy Learning <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p></font></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[LINQ with best example - 2]]></title>
<link>http://codingsense.wordpress.com/2009/01/12/linq-with-best-example-2/</link>
<pubDate>Mon, 12 Jan 2009 06:03:45 +0000</pubDate>
<dc:creator>codingsense</dc:creator>
<guid>http://codingsense.wordpress.com/2009/01/12/linq-with-best-example-2/</guid>
<description><![CDATA[Hi friends, In my previous post LINQ with best example &#8211; 1 , we had seen the basic concepts of]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Hi friends,<br />
In my previous post <a href="http://codingsense.wordpress.com/2008/09/08/better-linq-with-better-example-1/">LINQ with best example &#8211; 1</a> , we had seen the basic concepts of linq with the company sample. Today we will move a bit furthur and see how LINQ can be used for an project handling Inventory.</p>
<p> </p>
<h1 style="text-align:center;"><span style="text-decoration:underline;"><span style="font-size:large;"><span style="color:#0000ff;">The Inventory Sample</span></span></span></h1>
<p><strong>Downloads:</strong><br />
<a href="http://www.box.net/shared/pq060u41kv">Download source code &#8211; 8 Kb</a></p>
<p>In inventory we will create 3 classes namely Customer, Item, and Sales. The Class diagram of the 3 classes are shown below.</p>
<div id="attachment_181" class="wp-caption alignnone" style="width: 138px"><a rel="attachment wp-att-181" href="http://codingsense.wordpress.com/2009/01/12/linq-with-best-example-2/classdiagram/"><img class="size-thumbnail wp-image-181" title="classdiagram" src="http://codingsense.wordpress.com/files/2009/01/classdiagram.jpg?w=128" alt="classdiagram" width="128" height="89" /></a><p class="wp-caption-text">Class Diagram</p></div>
<p><strong>Create Customers, Items and Sales:</strong><br />
First let us insert some dummy records into the list of customer, Items and sales.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#2b91af;font-size:x-small;">List</span><span style="font-size:x-small;">&#60;</span><span style="color:#2b91af;font-size:x-small;">Customer</span><span style="font-size:x-small;">&#62; ListOfCustomer = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">List</span><span style="font-size:x-small;">&#60;</span><span style="color:#2b91af;font-size:x-small;">Customer</span><span style="font-size:x-small;">&#62;();</span>
<span style="color:#2b91af;font-size:x-small;">Customer</span><span style="font-size:x-small;"> C1 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Customer</span><span style="font-size:x-small;">() { Name = </span><span style="color:#a31515;font-size:x-small;">"Naveen"</span><span style="font-size:x-small;">, Area = </span><span style="color:#a31515;font-size:x-small;">"Mahalaxmi"</span><span style="font-size:x-small;">, City = </span><span style="color:#a31515;font-size:x-small;">"Bangalore"</span><span style="font-size:x-small;">, ID = 1 };</span>
<span style="color:#2b91af;font-size:x-small;">Customer</span><span style="font-size:x-small;"> C2 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Customer</span><span style="font-size:x-small;">() { Name = </span><span style="color:#a31515;font-size:x-small;">"Aatish"</span><span style="font-size:x-small;">, Area = </span><span style="color:#a31515;font-size:x-small;">"Layout"</span><span style="font-size:x-small;">, City = </span><span style="color:#a31515;font-size:x-small;">"Bangalore"</span><span style="font-size:x-small;">, ID = 2 };</span>
<span style="color:#2b91af;font-size:x-small;">Customer</span><span style="font-size:x-small;"> C3 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Customer</span><span style="font-size:x-small;">() { Name = </span><span style="color:#a31515;font-size:x-small;">"Manas"</span><span style="font-size:x-small;">, Area = </span><span style="color:#a31515;font-size:x-small;">"Layout"</span><span style="font-size:x-small;">, City = </span><span style="color:#a31515;font-size:x-small;">"Bangalore"</span><span style="font-size:x-small;">, ID = 3};</span>
<span style="color:#2b91af;font-size:x-small;">Customer</span><span style="font-size:x-small;"> C4 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Customer</span><span style="font-size:x-small;">() { Name = </span><span style="color:#a31515;font-size:x-small;">"Raghu"</span><span style="font-size:x-small;">, Area = </span><span style="color:#a31515;font-size:x-small;">"Mahalaxmi"</span><span style="font-size:x-small;">, City = </span><span style="color:#a31515;font-size:x-small;">"Bombay"</span><span style="font-size:x-small;">, ID = 4 };
ListOfCustomer.AddRange(</span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Customer</span><span style="font-size:x-small;">[]{C1,C2,C3,C4});</span>

<span style="color:#2b91af;font-size:x-small;">List</span><span style="font-size:x-small;">&#60;</span><span style="color:#2b91af;font-size:x-small;">Item</span><span style="font-size:x-small;">&#62; ListOfItems = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">List</span><span style="font-size:x-small;">&#60;</span><span style="color:#2b91af;font-size:x-small;">Item</span><span style="font-size:x-small;">&#62;();</span>
<span style="color:#2b91af;font-size:x-small;">Item</span><span style="font-size:x-small;"> I1 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Item</span><span style="font-size:x-small;">() { ID = 1, Name = </span><span style="color:#a31515;font-size:x-small;">"Monitor"</span><span style="font-size:x-small;">, Quantity = 15, UnitPrice = 5000 };</span>
<span style="color:#2b91af;font-size:x-small;">Item</span><span style="font-size:x-small;"> I2 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Item</span><span style="font-size:x-small;">() { ID = 2, Name = </span><span style="color:#a31515;font-size:x-small;">"Processor"</span><span style="font-size:x-small;">, Quantity = 10, UnitPrice = 10000};</span>
<span style="color:#2b91af;font-size:x-small;">Item</span><span style="font-size:x-small;"> I3 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Item</span><span style="font-size:x-small;">() { ID = 3, Name = </span><span style="color:#a31515;font-size:x-small;">"Keyboard"</span><span style="font-size:x-small;">, Quantity = 25 ,UnitPrice = 1000};
ListOfItems.AddRange(</span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Item</span><span style="font-size:x-small;">[]{I1,I2,I3});</span>

<span style="color:#2b91af;font-size:x-small;">List</span><span style="font-size:x-small;">&#60;</span><span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;">&#62; ListOfSales = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">List</span><span style="font-size:x-small;">&#60;</span><span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;">&#62;();</span>
<span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;"> S1 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;">() { CustomerID = 1, ItemID = 1, Quantity = 5 };</span>
<span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;"> S2 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;">() { CustomerID = 1, ItemID = 2, Quantity = 3 };</span>
<span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;"> S3 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;">() { CustomerID = 2, ItemID = 1, Quantity = 5 };</span>
<span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;"> S4 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;">() { CustomerID = 2, ItemID = 3, Quantity = 5 };</span>
<span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;"> S5 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;">() { CustomerID = 3, ItemID = 2, Quantity = 5 };</span>
<span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;"> S6 = </span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;">() { CustomerID = 3, ItemID = 3, Quantity = 15};
ListOfSales.AddRange(</span><span style="color:#0000ff;font-size:x-small;">new </span><span style="color:#2b91af;font-size:x-small;">Sales</span><span style="font-size:x-small;">[]{S1,S2,S3,S4,S5,S6});</span></pre>
<p>Using the above classes we will query the following</p>
<ul>
<li>List all customers</li>
<li>List customers from a specific city</li>
<li>No of customers in each city</li>
<li>No of items purchased by each customer</li>
<li>No of Items remaining in inventory</li>
<li>Amount received from each item</li>
<li>Amount paid by each customer</li>
</ul>
<p><strong>List all customers:</strong><br />
For listing the customers use the following code.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:x-small;">foreach</span><span style="font-size:x-small;"> (</span><span style="color:#2b91af;font-size:x-small;">Customer</span><span style="font-size:x-small;"> c </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfCustomer)
{</span>
<span style="color:#2b91af;font-size:x-small;">Console</span><span style="font-size:x-small;">.WriteLine(c.Name + </span><span style="color:#a31515;font-size:x-small;">","</span><span style="font-size:x-small;"> + c.ID + </span><span style="color:#a31515;font-size:x-small;">","</span><span style="font-size:x-small;"> + c.Area + </span><span style="color:#a31515;font-size:x-small;">","</span><span style="font-size:x-small;"> + c.City);
}</span></pre>
<p><strong>List customers from a specific city:</strong><br />
For filltering the customer depending on a specific city you can use</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:x-small;">var</span><span style="font-size:x-small;"> CustomerCity = </span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> customer </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfCustomer</span>
<span style="color:#0000ff;font-size:x-small;">where</span><span style="font-size:x-small;"> customer.City == </span><span style="color:#a31515;font-size:x-small;">"Bangalore"                               </span>
<span style="color:#0000ff;font-size:x-small;">select <span style="color:#0000ff;font-size:x-small;">new                               </span></span>
<span style="font-size:x-small;">{
Name = customer.Name,
ID = customer.ID
};</span></pre>
<p>You can also use  <span style="color:#0000ff;font-size:x-small;">where</span><span style="font-size:x-small;"> customer.City.StartsWith(</span><span style="color:#a31515;font-size:x-small;">&#8220;Ba&#8221;</span><span style="font-size:x-small;">)</span> to filter a customer when there is an entry in Quick search box.</p>
<p><strong>No of customers in each city:</strong><br />
For finding the No. of customers from each city we have to group the customers on key &#8216;CITY&#8217;.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:x-small;">var</span><span style="font-size:x-small;"> CustomersInCity = </span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> customer </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfCustomer</span>
<span style="color:#0000ff;font-size:x-small;">group</span><span style="font-size:x-small;"> customer </span><span style="color:#0000ff;font-size:x-small;">by</span><span style="font-size:x-small;"> customer.City </span><span style="color:#0000ff;font-size:x-small;">into</span><span style="font-size:x-small;"> GroupedCustomer</span>
<span style="color:#0000ff;font-size:x-small;">select <span style="color:#0000ff;font-size:x-small;">new                               </span></span>
<span style="font-size:x-small;">{
City = GroupedCustomer.Key,
EmployeeCount = GroupedCustomer.Count()
};</span></pre>
<p><strong>No of items purchased by each customer:</strong><br />
For searching the items purchased by each customer we shall join all 3 lists and derive the output.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:x-small;">var</span><span style="font-size:x-small;"> CustomerItem = </span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> cust </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfCustomer</span>
<span style="color:#0000ff;font-size:x-small;">join</span><span style="font-size:x-small;"> sale </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfSales </span>
<span style="color:#0000ff;font-size:x-small;">on</span><span style="font-size:x-small;"> cust.ID </span><span style="color:#0000ff;font-size:x-small;">equals</span><span style="font-size:x-small;"> sale.CustomerID </span>
<span style="color:#0000ff;font-size:x-small;">join</span><span style="font-size:x-small;"> item </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfItems</span>
<span style="color:#0000ff;font-size:x-small;">on</span><span style="font-size:x-small;"> sale.ItemID </span><span style="color:#0000ff;font-size:x-small;">equals</span><span style="font-size:x-small;"> item.ID </span>
<span style="color:#0000ff;font-size:x-small;">select <span style="color:#0000ff;font-size:x-small;">new </span></span><span style="font-size:x-small;">{ CustName = cust.Name, ItmName = item.Name, Qnt = sale.Quantity };</span></pre>
<p><strong>No of Items remaining in inventory:</strong><br />
For finding the remaining items in inventory we shall group the items in sales with itemid key. After which, we get quantity of each item sold. Then we will join the output of ItemID and SoldItems with Items in which we will get the No of items purchased. Subtracting Initial Count with Sold Count we get the items in Inventory.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:x-small;">var</span><span style="font-size:x-small;"> RemainingInventory = </span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> itm </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfItems</span>
<span style="color:#0000ff;font-size:x-small;">join</span><span style="font-size:x-small;"> sold </span><span style="color:#0000ff;font-size:x-small;">in</span>
<span style="font-size:x-small;">                                      (</span>
<span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> sale </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfSales</span>
<span style="color:#0000ff;font-size:x-small;">group</span><span style="font-size:x-small;"> sale </span><span style="color:#0000ff;font-size:x-small;">by</span><span style="font-size:x-small;"> sale.ItemID </span><span style="color:#0000ff;font-size:x-small;">into</span><span style="font-size:x-small;"> GroupedItemInSale</span>
<span style="color:#0000ff;font-size:x-small;">select <span style="color:#0000ff;font-size:x-small;">new                                          </span></span>
<span style="font-size:x-small;">{
ID = GroupedItemInSale.Key,
Qty = GroupedItemInSale.Sum(sold =&#62; sold.Quantity)
})</span>
<span style="color:#0000ff;font-size:x-small;">on</span><span style="font-size:x-small;"> itm.ID </span><span style="color:#0000ff;font-size:x-small;">equals</span><span style="font-size:x-small;"> sold.ID</span>
<span style="color:#0000ff;font-size:x-small;">select <span style="color:#0000ff;font-size:x-small;">new </span></span><span style="font-size:x-small;">{ itm.Name, RemQnt = itm.Quantity - sold.Qty };</span></pre>
<p><strong>Amount received from each item:</strong><br />
For getting the amount received from each item, first we require the total no of each item sold, so we shall group the sales on ItemId. By this we get the ItemID and no of items sold. Then the unit Pice of each item can be multiplied by the no of peices sold by joining the first query output with Item List.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:x-small;">var</span><span style="font-size:x-small;"> IncomeFromSales = </span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> item </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfItems</span>
<span style="color:#0000ff;font-size:x-small;">join</span><span style="font-size:x-small;"> sold </span><span style="color:#0000ff;font-size:x-small;">in</span>
<span style="font-size:x-small;">                                      (</span>
<span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> sale </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfSales</span>
<span style="color:#0000ff;font-size:x-small;">group</span><span style="font-size:x-small;"> sale </span><span style="color:#0000ff;font-size:x-small;">by</span><span style="font-size:x-small;"> sale.ItemID </span><span style="color:#0000ff;font-size:x-small;">into</span><span style="font-size:x-small;"> GroupedItems</span>
<span style="color:#0000ff;font-size:x-small;">select <span style="color:#0000ff;font-size:x-small;">new                                          </span></span>
<span style="font-size:x-small;">{
ID = GroupedItems.Key,
Quantity = GroupedItems.Sum(GI =&#62; GI.Quantity)
})</span>
<span style="color:#0000ff;font-size:x-small;">on</span><span style="font-size:x-small;"> item.ID </span><span style="color:#0000ff;font-size:x-small;">equals</span><span style="font-size:x-small;"> sold.ID </span>
<span style="color:#0000ff;font-size:x-small;">select <span style="color:#0000ff;font-size:x-small;">new </span></span><span style="font-size:x-small;">{ Name = item.Name, AmountRec = item.UnitPrice * sold.Quantity };</span></pre>
<p><strong>Amount paid by each customer:</strong><br />
For this first let us analyze what is required and then split it till we go to the solution<br />
1) Customer Name with Total Amount &#8211; the final output we require is Customer Name and Total Amount<br />
2) ListOfcustomer, (Customer ID, each item quantity , each item price) &#8211; Customer Name can be fetched by joining LIstofcustomer with an output that has (customerID, Quantity of each item purchased and  item price for each)<br />
3) ListOfCustomer, Joining(Item and Sales) &#8211; to attain the output for (2) can be attained if we join Item and sales.</p>
<p>So now reverse the process and the following query will be acheived.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:x-small;">var</span><span style="font-size:x-small;"> AmountPaidByCustomers = </span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> cust </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfCustomer</span>
<span style="color:#0000ff;font-size:x-small;">join</span><span style="font-size:x-small;"> GroupedAmount </span><span style="color:#0000ff;font-size:x-small;">in</span>
<span style="font-size:x-small;">(</span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> GroupAmount </span><span style="color:#0000ff;font-size:x-small;">in</span>
<span style="font-size:x-small;">(</span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> itm </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfItems</span>
<span style="color:#0000ff;font-size:x-small;">join</span><span style="font-size:x-small;"> sale </span><span style="color:#0000ff;font-size:x-small;">in</span><span style="font-size:x-small;"> ListOfSales</span>
<span style="color:#0000ff;font-size:x-small;">on</span><span style="font-size:x-small;"> itm.ID </span><span style="color:#0000ff;font-size:x-small;">equals</span><span style="font-size:x-small;"> sale.ItemID</span>
<span style="color:#0000ff;font-size:x-small;">select <span style="color:#0000ff;font-size:x-small;">new                                              </span></span>
<span style="font-size:x-small;">{
sale.CustomerID,
Amount = sale.Quantity * itm.UnitPrice
})</span>
<span style="color:#0000ff;font-size:x-small;">group</span><span style="font-size:x-small;"> GroupAmount </span><span style="color:#0000ff;font-size:x-small;">by</span><span style="font-size:x-small;"> GroupAmount.CustomerID </span><span style="color:#0000ff;font-size:x-small;">into</span><span style="font-size:x-small;"> GroupedAmount</span>
<span style="color:#0000ff;font-size:x-small;">select <span style="color:#0000ff;font-size:x-small;">new                                      </span></span>
<span style="font-size:x-small;">{CustomerID = GroupedAmount.Key,
Amount = GroupedAmount.Sum(i =&#62; i.Amount)
})</span>
<span style="color:#0000ff;font-size:x-small;">on</span><span style="font-size:x-small;"> cust.ID </span><span style="color:#0000ff;font-size:x-small;">equals</span><span style="font-size:x-small;"> GroupedAmount.CustomerID</span>
<span style="color:#0000ff;font-size:x-small;">select <span style="color:#0000ff;font-size:x-small;">new                              </span></span>
<span style="font-size:x-small;">{cust.Name,GroupedAmount.Amount};</span></pre>
<p>The above analysis helped in identifying how to proceed, So the steps followed to attain the final output<br />
1) Join Sales and Item -&#62; Get CustomerID, Amount = ItemID * Unit Pice ( this will be the amount for each type of item purchased)<br />
2) Group the output of (1) on CustomerID -&#62; Gets CustomerID, Amount ( this will be the total amount Purchased by the customer)<br />
3) Join result of (2) with LIstOfCustomer on ID -&#62; Gets Customer Name and Amount</p>
<p><strong>Output :</strong></p>
<p><strong></strong></p>
<p><strong></strong></p>
<p><strong></p>
<div id="attachment_183" class="wp-caption alignnone" style="width: 69px"><a rel="attachment wp-att-183" href="http://codingsense.wordpress.com/2009/01/12/linq-with-best-example-2/finaloutput/"><img class="size-thumbnail wp-image-183" title="finaloutput" src="http://codingsense.wordpress.com/files/2009/01/finaloutput.jpg?w=59" alt="Final Output" width="59" height="96" /></a><p class="wp-caption-text">Final Output</p></div>
<p></strong></p>
<p> </p>
<p> </p>
<p>By the last problem we come to know that problem are simple to solve, but you should analyze the problems and split it up in simple steps.<br />
Hope you have understood the Inventory sample, if you have any doubts or want a different sample then you can post a comment.</p>
<p> </p>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:x-small;">Happy Learning <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
</span></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Linq to Gac : Use Linq to Power Query your Gac via this C# Bridge to Fusion]]></title>
<link>http://team.pushbomb.com/2008/09/20/fusion_c_sharp_wrapper_for_linq_to_gac_access/</link>
<pubDate>Sun, 21 Sep 2008 00:43:45 +0000</pubDate>
<dc:creator>Damon Wilder Carr</dc:creator>
<guid>http://team.pushbomb.com/2008/09/20/fusion_c_sharp_wrapper_for_linq_to_gac_access/</guid>
<description><![CDATA[This post gets you Linq access to the GAC with no fuss. Fusion is wrapped for you so no need to unde]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><a href="http://dcarr.files.wordpress.com/2008/09/dfrac222.jpg"><img style="display:inline;border-width:0;margin:10px 0 10px 10px;" title="your gac" src="http://dcarr.files.wordpress.com/2008/09/dfrac222-thumb.jpg?w=246" border="0" alt="your gac" width="246" align="right" /></a></p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblog.domaindotnet.com%2f2008%2f09%2f20%2ffusion_c_sharp_wrapper_for_linq_to_gac_access%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblog.domaindotnet.com%2f2008%2f09%2f20%2ffusion_c_sharp_wrapper_for_linq_to_gac_access%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<p><a href="http://digg.com/submit?url=http%3a%2f%2fdcarr.wordpress.com%2f2008%2f09%2f20%2ffusion_c_sharp_wrapper_for_linq_to_gac_access%2f&#38;title=%e2%80%98Linq+to+Gac%e2%80%99+%3a+Use+Linq+to+Power+Query+your+Gac+via+this+C%23+Bridge+to+Fusion"><img style="border-width:0;" title="Digg This" src="http://digg.com/img/badges/100x20-digg-button.png" border="0" alt="Digg This" width="100" /></a></p>
<h4>This post gets you <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query" target="_blank">Linq</a> access to the GAC with no fuss.</h4>
<p>Fusion is wrapped for you so no need to understand the unmanaged stuff unless you want to. Your up and running with GAC as a new Linq to Objects source in minutes.</p>
<h2>The Big Picture of the Project (You can skip to the next section if you just want the GAC code)</h2>
<p>Note: The reason both the Gac and MyEnvironment IEnumerable&#60;T&#62; references below are different? See the reference on the bottom right of the picture into the entry point to all of it (ThisBox)?</p>
<p><a href="http://dcarr.files.wordpress.com/2008/09/classdiagram11.png"><img style="display:inline;margin-left:0;margin-right:0;" title="ClassDiagram1" src="http://dcarr.files.wordpress.com/2008/09/classdiagram1-thumb1.png?w=703" alt="ClassDiagram1" width="703" /></a></p>
<h2>Introduction to the Fusion Wrapped IEnumerable</h2>
<p>A simple case. I want to know how my GAC is broken into ‘Major Vendors’. We assume this is roughly the first part of the name. (i.e. Microsoft.System = Microsoft).</p>
<p>Oh yeah. Exclude the assemblies that only occur once. We are looking for the larger view here.</p>
<p>Here is an easy solution:</p>
<pre class="code"><span style="font-size:x-small;"><span style="background:black none repeat scroll 0 0;color:white;">        [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Fact</span></span><span style="font-size:x-small;"><span style="background:black none repeat scroll 0 0;color:white;">]
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public void </span></span><span style="font-size:x-small;"><span style="background:black none repeat scroll 0 0;color:white;">should_show_gac_by_vendor()
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">result = </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">from </span><span style="background:black none repeat scroll 0 0;color:white;">matchedAssembly </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">in </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">ThisBox</span><span style="background:black none repeat scroll 0 0;color:white;">.By&#60;</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">AssemblyDetail</span></span><span style="font-size:x-small;"><span style="background:black none repeat scroll 0 0;color:white;">&#62;()
                         </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">group </span><span style="background:black none repeat scroll 0 0;color:white;">matchedAssembly </span></span><span style="background:black none repeat scroll 0 0;color:#ecc03e;"><span style="font-size:x-small;">by
                             </span></span><span style="font-size:x-small;"><span style="background:black none repeat scroll 0 0;color:white;">matchedAssembly.MajorName
                             </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">into </span></span><span style="font-size:x-small;"><span style="background:black none repeat scroll 0 0;color:white;">grouping
                             </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">where </span></span><span style="font-size:x-small;"><span style="background:black none repeat scroll 0 0;color:white;">grouping.Count() &#62; 1
                             </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">orderby </span><span style="background:black none repeat scroll 0 0;color:white;">grouping.Count() </span></span><span style="background:black none repeat scroll 0 0;color:#ecc03e;"><span style="font-size:x-small;">descending
                             select </span></span><span style="font-size:x-small;"><span style="background:black none repeat scroll 0 0;color:white;">grouping;

            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span></span><span style="font-size:x-small;"><span style="background:black none repeat scroll 0 0;color:white;">TotalSum = result.Sum(ad =&#62; ad.Count());

            </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Console</span><span style="background:black none repeat scroll 0 0;color:white;">.WriteLine(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"** ASSEMBLIES BY NAME WITH &#62; 1 REGISTERED  = {0} ***"</span></span><span style="font-size:x-small;"><span style="background:black none repeat scroll 0 0;color:white;">, TotalSum);

            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">foreach </span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">firstGroup </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">in </span></span><span style="font-size:x-small;"><span style="background:black none repeat scroll 0 0;color:white;">result)
            {
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span></span><span style="font-size:x-small;"><span style="background:black none repeat scroll 0 0;color:white;">groupCount = firstGroup.Count();
                </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Console</span><span style="background:black none repeat scroll 0 0;color:white;">.WriteLine(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"Group '{0}' Assemblies={1} Percent={2} Public Keys Used={3}"</span></span><span style="background:black none repeat scroll 0 0;color:white;"><span style="font-size:x-small;">,
                                    firstGroup.Key,
                                    groupCount,
                                    groupCount.ToPctString(TotalSum),
                                    firstGroup.Select(ad =&#62; ad.PublicKey).Distinct().Count()
                                    );
            }
        }</span></span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h2>** ASSEMBLIES BY NAME WITH &#62; 1 REGISTERED  = 664 ***</h2>
<p>Group &#8216;Microsoft&#8217; Assemblies=466 Percent=70.18% Public Keys Used=5<br />
Group &#8216;DevExpress&#8217; Assemblies=73 Percent=10.99% Public Keys Used=2<br />
Group &#8216;System&#8217; Assemblies=71 Percent=10.69% Public Keys Used=4<br />
Group &#8216;Policy&#8217; Assemblies=14 Percent=2.11% Public Keys Used=2<br />
Group &#8216;FSharp&#8217; Assemblies=11 Percent=1.66% Public Keys Used=1<br />
Group &#8216;VLinq&#8217; Assemblies=10 Percent=1.51% Public Keys Used=1<br />
Group &#8216;policy&#8217; Assemblies=6 Percent=0.9% Public Keys Used=2<br />
Group &#8216;PresentationFramework&#8217; Assemblies=5 Percent=0.75% Public Keys Used=1<br />
Group &#8216;mscorcfg&#8217; Assemblies=2 Percent=0.3% Public Keys Used=1<br />
Group &#8216;WebDev&#8217; Assemblies=2 Percent=0.3% Public Keys Used=1<br />
Group &#8216;VSTADTEProvider&#8217; Assemblies=2 Percent=0.3% Public Keys Used=1<br />
Group &#8216;VsWebSite&#8217; Assemblies=2 Percent=0.3% Public Keys Used=1</p>
<p><span style="font-size:x-small;"><span style="font-size:x-small;">Try that with GacUtil (grin)..</span></span></p>
<h2>C# GAC IEnumerable&#60;Assembly&#62; implementation</h2>
<p>Ah yes the GAC Fusion wrapper code.  Thanks to <a href="http://dotnetjunkies.com/WebLog/debasish/" target="_blank">Debasish Bose</a> for his help here.</p>
<h1>C# Fusion Gac Enumerable : <a href="http://blog.domaindotnet.com/">Domain.Dot.Net</a> Team</h1>
<p><a href="http://dcarr.files.wordpress.com/2008/09/classdiagram21.png"><img style="display:inline;border-width:0;" title="ClassDiagram2" src="http://dcarr.files.wordpress.com/2008/09/classdiagram2-thumb1.png?w=684" border="0" alt="ClassDiagram2" width="684" /></a></p>
<pre class="code"><span style="background:black none repeat scroll 0 0;color:white;">    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
    /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Just to have some room to breath
    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public class </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">AssemblyDetail </span><span style="background:black none repeat scroll 0 0;color:white;">: </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">AssemblyDetailBase
    </span><span style="background:black none repeat scroll 0 0;color:white;">{
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public </span><span style="background:black none repeat scroll 0 0;color:white;">AssemblyDetail(</span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName </span><span style="background:black none repeat scroll 0 0;color:white;">currentAssembly) : </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">base</span><span style="background:black none repeat scroll 0 0;color:white;">(currentAssembly) {}

        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public override </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">String </span><span style="background:black none repeat scroll 0 0;color:white;">FullName
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get
            </span><span style="background:black none repeat scroll 0 0;color:white;">{
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">_fullName;
            }
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public override string </span><span style="background:black none repeat scroll 0 0;color:white;">Version
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get
            </span><span style="background:black none repeat scroll 0 0;color:white;">{
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">_versionString;
            }
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public override int </span><span style="background:black none repeat scroll 0 0;color:white;">Major
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get
            </span><span style="background:black none repeat scroll 0 0;color:white;">{
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">VersionComponent(0);
            }
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public override int </span><span style="background:black none repeat scroll 0 0;color:white;">Minor
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get
            </span><span style="background:black none repeat scroll 0 0;color:white;">{
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">VersionComponent(1);
            }
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public override int </span><span style="background:black none repeat scroll 0 0;color:white;">Revision
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get
            </span><span style="background:black none repeat scroll 0 0;color:white;">{
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">VersionComponent(2);
            }
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public override int </span><span style="background:black none repeat scroll 0 0;color:white;">Build
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get
            </span><span style="background:black none repeat scroll 0 0;color:white;">{
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">VersionComponent(3);
            }
        }

        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public override string </span><span style="background:black none repeat scroll 0 0;color:white;">PublicKey
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get
            </span><span style="background:black none repeat scroll 0 0;color:white;">{
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">_pkey;
            }
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public override string </span><span style="background:black none repeat scroll 0 0;color:white;">MajorName
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get
            </span><span style="background:black none repeat scroll 0 0;color:white;">{
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">_majorName;
            }
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">String </span><span style="background:black none repeat scroll 0 0;color:white;">Name
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get
            </span><span style="background:black none repeat scroll 0 0;color:white;">{
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">_fullName;
            }
        }

        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">VersionComponent(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">position)
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">_version.Length &#60;= position + 1
                           ? </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Convert</span><span style="background:black none repeat scroll 0 0;color:white;">.ToInt32(_version[position]) : 0;
        }
    }</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="background:black none repeat scroll 0 0;color:white;">    #region

    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System;
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System.Runtime.InteropServices;

    #endregion

    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
    /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Alpha - Comment and Add Regressions
    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public abstract class </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">AssemblyDetailBase
    </span><span style="background:black none repeat scroll 0 0;color:white;">{
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">protected string </span><span style="background:black none repeat scroll 0 0;color:white;">_fullName;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">protected string </span><span style="background:black none repeat scroll 0 0;color:white;">_majorName;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">string</span><span style="background:black none repeat scroll 0 0;color:white;">[] _nameSplit;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">protected string </span><span style="background:black none repeat scroll 0 0;color:white;">_pkey;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">string </span><span style="background:black none repeat scroll 0 0;color:white;">_rawname;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">protected string</span><span style="background:black none repeat scroll 0 0;color:white;">[] _version;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">protected string </span><span style="background:black none repeat scroll 0 0;color:white;">_versionString;

        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
        /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Initializes a new instance of the </span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;see cref=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"AssemblyDetailBase"</span><span style="background:black none repeat scroll 0 0;color:gray;">/&#62; </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">class.
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
        /// &#60;param name=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"Current"</span><span style="background:black none repeat scroll 0 0;color:gray;">&#62;</span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">The current.</span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;/param&#62;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">protected </span><span style="background:black none repeat scroll 0 0;color:white;">AssemblyDetailBase(</span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName </span><span style="background:black none repeat scroll 0 0;color:white;">Current)
        {
            ExtractParsedDetail(Current);
        }

        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public abstract string </span><span style="background:black none repeat scroll 0 0;color:white;">FullName
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get</span><span style="background:black none repeat scroll 0 0;color:white;">;
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public abstract string </span><span style="background:black none repeat scroll 0 0;color:white;">Version
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get</span><span style="background:black none repeat scroll 0 0;color:white;">;
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public abstract int </span><span style="background:black none repeat scroll 0 0;color:white;">Major
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get</span><span style="background:black none repeat scroll 0 0;color:white;">;
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public abstract int </span><span style="background:black none repeat scroll 0 0;color:white;">Minor
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get</span><span style="background:black none repeat scroll 0 0;color:white;">;
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public abstract int </span><span style="background:black none repeat scroll 0 0;color:white;">Revision
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get</span><span style="background:black none repeat scroll 0 0;color:white;">;
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public abstract int </span><span style="background:black none repeat scroll 0 0;color:white;">Build
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get</span><span style="background:black none repeat scroll 0 0;color:white;">;
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public abstract string </span><span style="background:black none repeat scroll 0 0;color:white;">PublicKey
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get</span><span style="background:black none repeat scroll 0 0;color:white;">;
        }
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public abstract string </span><span style="background:black none repeat scroll 0 0;color:white;">MajorName
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get</span><span style="background:black none repeat scroll 0 0;color:white;">;
        }

        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
        /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">I do not want to persist the IAssemblyName
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">as it might introduce a COM reference when
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">we just can extract what we need and let it
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">pass away into whatever...
        </span><span style="background:black none repeat scroll 0 0;color:gray;">///
        /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Extracts the parsed detail.
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
        /// &#60;param name=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"Current"</span><span style="background:black none repeat scroll 0 0;color:gray;">&#62;</span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">The current.</span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;/param&#62;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">void </span><span style="background:black none repeat scroll 0 0;color:white;">ExtractParsedDetail(</span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName </span><span style="background:black none repeat scroll 0 0;color:white;">Current)
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">const int </span><span style="background:black none repeat scroll 0 0;color:white;">stongKeyPosition = 3;
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">const int </span><span style="background:black none repeat scroll 0 0;color:white;">versionPosition = 1;

            </span><span style="background:black none repeat scroll 0 0;color:#bcf5c9;">// Main Work Happens Here
            </span><span style="background:black none repeat scroll 0 0;color:white;">_fullName = GetDisplayName(Current);

            </span><span style="background:black none repeat scroll 0 0;color:#bcf5c9;">// Simply parse the ", " that seperates the full
            // name
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">_parsedDetails = _fullName.Split(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">new</span><span style="background:black none repeat scroll 0 0;color:white;">[]
                                                 {
                                                         </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">", "
                                                 </span><span style="background:black none repeat scroll 0 0;color:white;">}, </span><span style="background:black none repeat scroll 0 0;color:#b8a223;">StringSplitOptions</span><span style="background:black none repeat scroll 0 0;color:white;">.RemoveEmptyEntries);

            ExtractVersionInfo(versionPosition, _parsedDetails);
            ExtractAddition(stongKeyPosition, _parsedDetails);
        }

        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">void </span><span style="background:black none repeat scroll 0 0;color:white;">ExtractAddition(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">stongKeyPosition, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">string</span><span style="background:black none repeat scroll 0 0;color:white;">[] parsedDetails)
        {
            _pkey = parsedDetails[stongKeyPosition];
            _rawname = parsedDetails[0].Trim();
            _nameSplit = _rawname.Split(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">'.'</span><span style="background:black none repeat scroll 0 0;color:white;">);
            _majorName = _nameSplit[0].Trim();
        }

        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
        /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Gets the display name from the unmanaged pointer
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
        /// &#60;param name=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"aName"</span><span style="background:black none repeat scroll 0 0;color:gray;">&#62;</span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">A name.</span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;/param&#62;
        /// &#60;returns&#62;&#60;/returns&#62;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">static string </span><span style="background:black none repeat scroll 0 0;color:white;">GetDisplayName(</span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName </span><span style="background:black none repeat scroll 0 0;color:white;">aName)
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">displayFlags = </span><span style="background:black none repeat scroll 0 0;color:#b8a223;">AssemblyNameDisplayFlags</span><span style="background:black none repeat scroll 0 0;color:white;">.ALL;

            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint </span><span style="background:black none repeat scroll 0 0;color:white;">uiLen = 0;

            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">string </span><span style="background:black none repeat scroll 0 0;color:white;">_displayAssemblyName = </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">null</span><span style="background:black none repeat scroll 0 0;color:white;">;

            </span><span style="background:black none repeat scroll 0 0;color:#bcf5c9;">// Get the length
            </span><span style="background:black none repeat scroll 0 0;color:white;">aName.GetDisplayName(</span><span style="background:black none repeat scroll 0 0;color:#76a38a;">IntPtr</span><span style="background:black none repeat scroll 0 0;color:white;">.Zero, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">ref </span><span style="background:black none repeat scroll 0 0;color:white;">uiLen, (</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint</span><span style="background:black none repeat scroll 0 0;color:white;">) displayFlags);

            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">if </span><span style="background:black none repeat scroll 0 0;color:white;">(uiLen &#62; 0)
            {
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">byteBuffer = </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">new byte</span><span style="background:black none repeat scroll 0 0;color:white;">[(uiLen + 1)*2];
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">unsafe </span><span style="background:black none repeat scroll 0 0;color:#bcf5c9;">//turn on sunsafe in project properties
                </span><span style="background:black none repeat scroll 0 0;color:white;">{
                    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">fixed </span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">byte</span><span style="background:black none repeat scroll 0 0;color:white;">* numRef1 = byteBuffer)
                    {
                        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">ptr1 = </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">new </span><span style="background:black none repeat scroll 0 0;color:#76a38a;">IntPtr</span><span style="background:black none repeat scroll 0 0;color:white;">(numRef1);
                        aName.GetDisplayName(ptr1, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">ref </span><span style="background:black none repeat scroll 0 0;color:white;">uiLen, (</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint</span><span style="background:black none repeat scroll 0 0;color:white;">) displayFlags);
                        _displayAssemblyName = </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Marshal</span><span style="background:black none repeat scroll 0 0;color:white;">.PtrToStringUni(ptr1);
                    }
                }
            }

            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">_displayAssemblyName;
        }

        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
        /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Extracts the version info.
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
        /// &#60;param name=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"versionPosition"</span><span style="background:black none repeat scroll 0 0;color:gray;">&#62;</span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">The version position.</span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;/param&#62;
        /// &#60;param name=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"parsedDetails"</span><span style="background:black none repeat scroll 0 0;color:gray;">&#62;</span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">The parsed details.</span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;/param&#62;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">void </span><span style="background:black none repeat scroll 0 0;color:white;">ExtractVersionInfo(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">versionPosition, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">string</span><span style="background:black none repeat scroll 0 0;color:white;">[] parsedDetails)
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">versionLiteral = parsedDetails[versionPosition];
            _versionString = ToCoreVersion(versionPosition, versionLiteral);
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">if </span><span style="background:black none repeat scroll 0 0;color:white;">(_versionString != </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">String</span><span style="background:black none repeat scroll 0 0;color:white;">.Empty)
                _version = _versionString.Split(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">'.'</span><span style="background:black none repeat scroll 0 0;color:white;">);
        }

        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
        /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Gets the nice clean x.x.x.x string
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
        /// &#60;param name=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"versionPosition"</span><span style="background:black none repeat scroll 0 0;color:gray;">&#62;</span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">The version position.</span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;/param&#62;
        /// &#60;param name=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"versionLiteral"</span><span style="background:black none repeat scroll 0 0;color:gray;">&#62;</span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">The version literal.</span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;/param&#62;
        /// &#60;returns&#62;&#60;/returns&#62;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">static string </span><span style="background:black none repeat scroll 0 0;color:white;">ToCoreVersion(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">versionPosition, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">string </span><span style="background:black none repeat scroll 0 0;color:white;">versionLiteral)
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">returnVal = </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">String</span><span style="background:black none repeat scroll 0 0;color:white;">.Empty;

            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">if </span><span style="background:black none repeat scroll 0 0;color:white;">(!</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">String</span><span style="background:black none repeat scroll 0 0;color:white;">.IsNullOrEmpty(versionLiteral))
            {
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">_indexOf = versionLiteral.IndexOf(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"="</span><span style="background:black none repeat scroll 0 0;color:white;">);

                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">if </span><span style="background:black none repeat scroll 0 0;color:white;">(_indexOf != -1)
                    returnVal = versionLiteral.Substring(_indexOf + versionPosition);
            }

            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">returnVal;
        }
    }</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="background:black none repeat scroll 0 0;color:white;">#region

</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System.Runtime.InteropServices;

#endregion

</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">class </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">GAC
</span><span style="background:black none repeat scroll 0 0;color:white;">{
    GAC() {}

    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">DllImport</span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"Fusion.dll"</span><span style="background:black none repeat scroll 0 0;color:white;">, CharSet = </span><span style="background:black none repeat scroll 0 0;color:#b8a223;">CharSet</span><span style="background:black none repeat scroll 0 0;color:white;">.Auto)]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public static extern int </span><span style="background:black none repeat scroll 0 0;color:white;">CreateAssemblyEnum(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyEnum </span><span style="background:black none repeat scroll 0 0;color:white;">ppEnum,
                                                </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IApplicationContext </span><span style="background:black none repeat scroll 0 0;color:white;">pAppCtx,
                                                </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName </span><span style="background:black none repeat scroll 0 0;color:white;">pName, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint </span><span style="background:black none repeat scroll 0 0;color:white;">dwFlags,
                                                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">pvReserved);
}</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="background:black none repeat scroll 0 0;color:white;">#region

</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System.Runtime.InteropServices;
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System.Security;

#endregion

[</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">SuppressUnmanagedCodeSecurity</span><span style="background:black none repeat scroll 0 0;color:white;">, </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">ComImport</span><span style="background:black none repeat scroll 0 0;color:white;">, </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Guid</span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"7C23FF90-33AF-11D3-95DA-00A024A85B51"</span><span style="background:black none repeat scroll 0 0;color:white;">),
 </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">InterfaceType</span><span style="background:black none repeat scroll 0 0;color:white;">(1)]
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public interface </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IApplicationContext
</span><span style="background:black none repeat scroll 0 0;color:white;">{
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">void </span><span style="background:black none repeat scroll 0 0;color:white;">SetContextNameObject(</span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName </span><span style="background:black none repeat scroll 0 0;color:white;">pName);

    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">void </span><span style="background:black none repeat scroll 0 0;color:white;">GetContextNameObject([</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Out</span><span style="background:black none repeat scroll 0 0;color:white;">] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName </span><span style="background:black none repeat scroll 0 0;color:white;">ppName);

    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">void </span><span style="background:black none repeat scroll 0 0;color:white;">Set([</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">MarshalAs</span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#b8a223;">UnmanagedType</span><span style="background:black none repeat scroll 0 0;color:white;">.LPWStr)] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">string </span><span style="background:black none repeat scroll 0 0;color:white;">szName, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">pvValue, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint </span><span style="background:black none repeat scroll 0 0;color:white;">cbValue,
             </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint </span><span style="background:black none repeat scroll 0 0;color:white;">dwFlags);

    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">void </span><span style="background:black none repeat scroll 0 0;color:white;">Get([</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">MarshalAs</span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#b8a223;">UnmanagedType</span><span style="background:black none repeat scroll 0 0;color:white;">.LPWStr)] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">string </span><span style="background:black none repeat scroll 0 0;color:white;">szName, [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Out</span><span style="background:black none repeat scroll 0 0;color:white;">] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out int </span><span style="background:black none repeat scroll 0 0;color:white;">pvValue,
             </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">ref uint </span><span style="background:black none repeat scroll 0 0;color:white;">pcbValue, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint </span><span style="background:black none repeat scroll 0 0;color:white;">dwFlags);

    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">void </span><span style="background:black none repeat scroll 0 0;color:white;">GetDynamicDirectory([</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Out</span><span style="background:black none repeat scroll 0 0;color:white;">] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out int </span><span style="background:black none repeat scroll 0 0;color:white;">wzDynamicDir, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">ref uint </span><span style="background:black none repeat scroll 0 0;color:white;">pdwSize);
}</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="background:black none repeat scroll 0 0;color:white;">#region

</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System;
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System.Runtime.InteropServices;
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System.Security;
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System.Text;

#endregion

</span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
/// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">IAssemblyName from Fusion
</span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
</span><span style="background:black none repeat scroll 0 0;color:white;">[</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">SuppressUnmanagedCodeSecurity</span><span style="background:black none repeat scroll 0 0;color:white;">, </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">ComImport</span><span style="background:black none repeat scroll 0 0;color:white;">, </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Guid</span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"CD193BC0-B4BC-11D2-9833-00C04FC31D2E"</span><span style="background:black none repeat scroll 0 0;color:white;">),
 </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">InterfaceType</span><span style="background:black none repeat scroll 0 0;color:white;">(1)]
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public interface </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName
</span><span style="background:black none repeat scroll 0 0;color:white;">{
    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">PreserveSig</span><span style="background:black none repeat scroll 0 0;color:white;">]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">SetProperty(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint </span><span style="background:black none repeat scroll 0 0;color:white;">PropertyId, </span><span style="background:black none repeat scroll 0 0;color:#76a38a;">IntPtr </span><span style="background:black none repeat scroll 0 0;color:white;">pvProperty, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint </span><span style="background:black none repeat scroll 0 0;color:white;">cbProperty);

    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">PreserveSig</span><span style="background:black none repeat scroll 0 0;color:white;">]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">GetProperty(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint </span><span style="background:black none repeat scroll 0 0;color:white;">PropertyId, </span><span style="background:black none repeat scroll 0 0;color:#76a38a;">IntPtr </span><span style="background:black none repeat scroll 0 0;color:white;">pvProperty, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">ref uint </span><span style="background:black none repeat scroll 0 0;color:white;">pcbProperty);

    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">PreserveSig</span><span style="background:black none repeat scroll 0 0;color:white;">]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">Finalize();

    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">PreserveSig</span><span style="background:black none repeat scroll 0 0;color:white;">]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">GetDisplayName(</span><span style="background:black none repeat scroll 0 0;color:#76a38a;">IntPtr </span><span style="background:black none repeat scroll 0 0;color:white;">szDisplayName, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">ref uint </span><span style="background:black none repeat scroll 0 0;color:white;">pccDisplayName, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint </span><span style="background:black none repeat scroll 0 0;color:white;">dwDisplayFlags);

    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">PreserveSig</span><span style="background:black none repeat scroll 0 0;color:white;">]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">BindToObject(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">object </span><span style="background:black none repeat scroll 0 0;color:white;">refIID, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">object </span><span style="background:black none repeat scroll 0 0;color:white;">pAsmBindSink,
                     </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IApplicationContext </span><span style="background:black none repeat scroll 0 0;color:white;">pApplicationContext,
                     [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">MarshalAs</span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#b8a223;">UnmanagedType</span><span style="background:black none repeat scroll 0 0;color:white;">.LPWStr)] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">string </span><span style="background:black none repeat scroll 0 0;color:white;">szCodeBase, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">long </span><span style="background:black none repeat scroll 0 0;color:white;">llFlags,
                     </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">pvReserved, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint </span><span style="background:black none repeat scroll 0 0;color:white;">cbReserved, [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Out</span><span style="background:black none repeat scroll 0 0;color:white;">] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out int </span><span style="background:black none repeat scroll 0 0;color:white;">ppv);

    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">PreserveSig</span><span style="background:black none repeat scroll 0 0;color:white;">]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">GetName(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">ref uint </span><span style="background:black none repeat scroll 0 0;color:white;">lpcwBuffer,
                [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Out</span><span style="background:black none repeat scroll 0 0;color:white;">, </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">MarshalAs</span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#b8a223;">UnmanagedType</span><span style="background:black none repeat scroll 0 0;color:white;">.LPWStr)] </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">StringBuilder </span><span style="background:black none repeat scroll 0 0;color:white;">pwzName);

    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">PreserveSig</span><span style="background:black none repeat scroll 0 0;color:white;">]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">GetVersion([</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Out</span><span style="background:black none repeat scroll 0 0;color:white;">] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out uint </span><span style="background:black none repeat scroll 0 0;color:white;">pdwVersionHi, [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Out</span><span style="background:black none repeat scroll 0 0;color:white;">] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out uint </span><span style="background:black none repeat scroll 0 0;color:white;">pdwVersionLow);

    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">PreserveSig</span><span style="background:black none repeat scroll 0 0;color:white;">]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">IsEqual(</span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName </span><span style="background:black none repeat scroll 0 0;color:white;">pName, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint </span><span style="background:black none repeat scroll 0 0;color:white;">dwCmpFlags);

    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">PreserveSig</span><span style="background:black none repeat scroll 0 0;color:white;">]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">Clone([</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Out</span><span style="background:black none repeat scroll 0 0;color:white;">] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName </span><span style="background:black none repeat scroll 0 0;color:white;">pName);
}</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="background:black none repeat scroll 0 0;color:white;">#region

</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System.Runtime.InteropServices;
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System.Security;

#endregion

[</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">SuppressUnmanagedCodeSecurity</span><span style="background:black none repeat scroll 0 0;color:white;">, </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">ComImport</span><span style="background:black none repeat scroll 0 0;color:white;">, </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Guid</span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"21B8916C-F28E-11D2-A473-00C04F8EF448"</span><span style="background:black none repeat scroll 0 0;color:white;">),
 </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">InterfaceType</span><span style="background:black none repeat scroll 0 0;color:white;">(1)]
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">interface </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyEnum
</span><span style="background:black none repeat scroll 0 0;color:white;">{
    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">PreserveSig</span><span style="background:black none repeat scroll 0 0;color:white;">]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">GetNextAssembly([</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Out</span><span style="background:black none repeat scroll 0 0;color:white;">] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IApplicationContext </span><span style="background:black none repeat scroll 0 0;color:white;">ppAppCtx,
                        [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Out</span><span style="background:black none repeat scroll 0 0;color:white;">] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName </span><span style="background:black none repeat scroll 0 0;color:white;">ppName, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint </span><span style="background:black none repeat scroll 0 0;color:white;">dwFlags);

    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">PreserveSig</span><span style="background:black none repeat scroll 0 0;color:white;">]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">Reset();

    [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">PreserveSig</span><span style="background:black none repeat scroll 0 0;color:white;">]
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">Clone([</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Out</span><span style="background:black none repeat scroll 0 0;color:white;">] </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyEnum </span><span style="background:black none repeat scroll 0 0;color:white;">ppEnum);
}</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="background:black none repeat scroll 0 0;color:white;">#region

</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System.Collections;
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System.Collections.Generic;
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">using </span><span style="background:black none repeat scroll 0 0;color:white;">System.Runtime.InteropServices;

#endregion

</span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
/// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Fairly basic stuff once the
</span><span style="background:black none repeat scroll 0 0;color:gray;">/// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">unmanaged stuff is all mapped
</span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public class </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">GacEnumerable </span><span style="background:black none repeat scroll 0 0;color:white;">: </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IEnumerator</span><span style="background:black none repeat scroll 0 0;color:white;">&#60;</span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName</span><span style="background:black none repeat scroll 0 0;color:white;">&#62;, </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IEnumerable</span><span style="background:black none repeat scroll 0 0;color:white;">&#60;</span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName</span><span style="background:black none repeat scroll 0 0;color:white;">&#62;
{
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">readonly </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyEnum </span><span style="background:black none repeat scroll 0 0;color:white;">_enumeration;
    </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName </span><span style="background:black none repeat scroll 0 0;color:white;">_enumCurrentAssembly;

    GacEnumerable()
    {
        HandleCom(</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">GAC</span><span style="background:black none repeat scroll 0 0;color:white;">.CreateAssemblyEnum(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out </span><span style="background:black none repeat scroll 0 0;color:white;">_enumeration, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">null</span><span style="background:black none repeat scroll 0 0;color:white;">, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">null</span><span style="background:black none repeat scroll 0 0;color:white;">,
                                         (</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">uint</span><span style="background:black none repeat scroll 0 0;color:white;">) </span><span style="background:black none repeat scroll 0 0;color:#b8a223;">AssemblyCacheFlags</span><span style="background:black none repeat scroll 0 0;color:white;">.GAC, 0));
    }

    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public static </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">GacEnumerable </span><span style="background:black none repeat scroll 0 0;color:white;">New
    {
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get
        </span><span style="background:black none repeat scroll 0 0;color:white;">{
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return new </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">GacEnumerable</span><span style="background:black none repeat scroll 0 0;color:white;">();
        }
    }

    #region IEnumerable&#60;IAssemblyName&#62; Members

    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
    /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Returns an enumerator that iterates through the collection.
    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
    /// &#60;returns&#62;
    /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">A </span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;see cref=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"T:System.Collections.Generic.IEnumerator`1"</span><span style="background:black none repeat scroll 0 0;color:gray;">/&#62; </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">that can be used to iterate through the collection.
    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/returns&#62;
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IEnumerator</span><span style="background:black none repeat scroll 0 0;color:white;">&#60;</span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName</span><span style="background:black none repeat scroll 0 0;color:white;">&#62; GetEnumerator()
    {
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return this</span><span style="background:black none repeat scroll 0 0;color:white;">;
    }

    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
    /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Returns an enumerator that iterates through a collection.
    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
    /// &#60;returns&#62;
    /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">An </span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;see cref=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"T:System.Collections.IEnumerator"</span><span style="background:black none repeat scroll 0 0;color:gray;">/&#62; </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">object that can be used to iterate through the collection.
    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/returns&#62;
    </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IEnumerator IEnumerable</span><span style="background:black none repeat scroll 0 0;color:white;">.GetEnumerator()
    {
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">GetEnumerator();
    }

    #endregion

    #region IEnumerator&#60;IAssemblyName&#62; Members

    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
    /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Advances the enumerator to the next element of the collection.
    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
    /// &#60;returns&#62;
    /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/returns&#62;
    /// &#60;exception cref=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"T:System.InvalidOperationException"</span><span style="background:black none repeat scroll 0 0;color:gray;">&#62;</span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">The collection was modified after the enumerator was created. </span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;/exception&#62;
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">bool </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IEnumerator</span><span style="background:black none repeat scroll 0 0;color:white;">.MoveNext()
    {
        </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IApplicationContext </span><span style="background:black none repeat scroll 0 0;color:white;">context1;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">(0 ==
                _enumeration.GetNextAssembly(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out </span><span style="background:black none repeat scroll 0 0;color:white;">context1, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">out </span><span style="background:black none repeat scroll 0 0;color:white;">_enumCurrentAssembly, 0));
    }

    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
    /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Sets the enumerator to its initial position, which is before the first element in the collection.
    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
    /// &#60;exception cref=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"T:System.InvalidOperationException"</span><span style="background:black none repeat scroll 0 0;color:gray;">&#62;</span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">The collection was modified after the enumerator was created. </span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;/exception&#62;
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">void </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IEnumerator</span><span style="background:black none repeat scroll 0 0;color:white;">.Reset()
    {
        _enumeration.Reset();
    }

    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
    /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">IAssemblyName is a Fusion interface
    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Gets the element in the collection at the current position of the enumerator.
    </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
    /// &#60;value&#62;&#60;/value&#62;
    /// &#60;returns&#62;</span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">The element in the collection at the current position of the enumerator.</span><span style="background:black none repeat scroll 0 0;color:gray;">&#60;/returns&#62;
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IAssemblyName </span><span style="background:black none repeat scroll 0 0;color:white;">Current
    {
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get
        </span><span style="background:black none repeat scroll 0 0;color:white;">{
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">_enumCurrentAssembly;
        }
    }
    </span><span style="background:black none repeat scroll 0 0;color:#bcf5c9;">// Properties
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">object </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IEnumerator</span><span style="background:black none repeat scroll 0 0;color:white;">.Current
    {
        </span><span style="background:black none repeat scroll 0 0;color:#bcf5c9;">// Go to the other darn Current not this one
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">get
        </span><span style="background:black none repeat scroll 0 0;color:white;">{
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">Current;
        }
    }
    </span><span style="background:black none repeat scroll 0 0;color:#bcf5c9;">// Fields
    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public void </span><span style="background:black none repeat scroll 0 0;color:white;">Dispose()
    {
        _enumCurrentAssembly = </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">null</span><span style="background:black none repeat scroll 0 0;color:white;">;
    }

    #endregion

    </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">internal static void </span><span style="background:black none repeat scroll 0 0;color:white;">HandleCom(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">hResult)
    {
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">if </span><span style="background:black none repeat scroll 0 0;color:white;">(hResult &#60; 0)
            </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Marshal</span><span style="background:black none repeat scroll 0 0;color:white;">.ThrowExceptionForHR(hResult);
    }
}</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><span style="font-size:x-small;"> </span></p>
<pre class="code"><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">[</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Flags</span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">]
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">enum </span></span><span style="background:black none repeat scroll 0 0;color:#b8a223;"><span style="font-size:x-small;font-family:Consolas;">AssemblyCacheFlags
</span></span><span style="background:black none repeat scroll 0 0;color:white;"><span style="font-size:x-small;font-family:Consolas;">{
    NGenCache = 0x1,
    GAC = 0x2,
    DownloadCache = 0x4
}</span></span></pre>
<p><span style="font-size:x-small;"><br />
</span></p>
<pre class="code"><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">[</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Flags</span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">]
</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public enum </span></span><span style="background:black none repeat scroll 0 0;color:#b8a223;"><span style="font-size:x-small;font-family:Consolas;">AssemblyNameDisplayFlags
</span></span><span style="background:black none repeat scroll 0 0;color:white;"><span style="font-size:x-small;font-family:Consolas;">{
    VERSION = 0x01,
    CULTURE = 0x02,
    PUBLIC_KEY_TOKEN = 0x04,
    PROCESSORARCHITECTURE = 0x20,
    RETARGETABLE = 0x80,
    ALL = VERSION &#124;
        CULTURE &#124; PROCESSORARCHITECTURE &#124;
        PUBLIC_KEY_TOKEN &#124; RETARGETABLE
}</span></span></pre>
<h2>More to Come</h2>
<p>This is the tip of the iceberg so to speak. Here is a test case to show you what this now is doing and perhaps you can give us ideas where to take it. There is a lot of code. Should it be up on CodePlex? Google Code?  Your Environment Variables:</p>
<pre class="code"><span style="background:black none repeat scroll 0 0;color:white;">    <span style="font-size:x-small;font-family:Consolas;">   [</span></span><span style="background:black none repeat scroll 0 0;color:#2b91af;"><span style="font-size:x-small;font-family:Consolas;">Fact</span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">]
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public void </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">should_show_my_env()
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">sw = </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Stopwatch</span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">.StartNew();
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">dirs = </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">from </span><span style="background:black none repeat scroll 0 0;color:white;">env </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">in </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">ThisBox</span><span style="background:black none repeat scroll 0 0;color:white;">.By&#60;</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">EnvironmentVariable</span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">&#62;()
                       </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">select </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">env;
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">foreach </span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">s </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">in </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">dirs)
            {
                </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Console</span><span style="background:black none repeat scroll 0 0;color:white;">.WriteLine(s.TypeOfEnv + </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">" " </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">+ s.KeyName);
                </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Console</span><span style="background:black none repeat scroll 0 0;color:white;">.WriteLine(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"  ==&#62;" </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">+ s.Value);
            }
            </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Console</span><span style="background:black none repeat scroll 0 0;color:white;">.WriteLine(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"Count : " </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">+ dirs.Count());
            </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Console</span><span style="background:black none repeat scroll 0 0;color:white;">.WriteLine(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"Search Took : " </span><span style="background:black none repeat scroll 0 0;color:white;">+ sw.ElapsedMilliseconds + </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">" ms"</span></span><span style="background:black none repeat scroll 0 0;color:white;"><span style="font-size:x-small;font-family:Consolas;">);
            sw.Stop();
        }</span></span></pre>
<p><span style="font-size:x-small;font-family:Consolas;">Let’s join our GAC with our registry!</span></p>
<p><span style="font-size:x-small;font-family:Consolas;">NOTE: we get 592 hits in 20 or so seconds… </span></p>
<pre class="code"><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">        [</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Fact</span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">]
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public void </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">should_join_policy_registry_section()
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">sw = </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Stopwatch</span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">.StartNew();

            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">result = </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:#ecc03e;">from
                             </span><span style="background:black none repeat scroll 0 0;color:white;">gac </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">in </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">ThisBox</span><span style="background:black none repeat scroll 0 0;color:white;">.By&#60;</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">AssemblyDetail</span></span><span style="background:black none repeat scroll 0 0;color:white;"><span style="font-size:x-small;font-family:Consolas;">&#62;()
                         </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:#ecc03e;">join
                             </span><span style="background:black none repeat scroll 0 0;color:white;">registryKey </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">in </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">ThisBox</span><span style="background:black none repeat scroll 0 0;color:white;">.By&#60;</span><span style="background:black none repeat scroll 0 0;color:#2b91af;">RegistryKey</span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">&#62;() 

                         </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">on </span><span style="background:black none repeat scroll 0 0;color:white;">gac.MajorName </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">equals </span></span><span style="background:black none repeat scroll 0 0;color:white;"><span style="font-size:x-small;font-family:Consolas;">registryKey.FirstRegValue()

                         </span></span><span style="background:black none repeat scroll 0 0;color:#ecc03e;"><span style="font-size:x-small;font-family:Consolas;">select new
                                </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">{
                                        gac, RegVal=registryKey.FirstRegValue()
                                };

            </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Console</span><span style="background:black none repeat scroll 0 0;color:white;">.WriteLine(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"Count : " </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">+ result.Count());
            sw.Stop();
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">foreach </span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">a </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">in </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">result)
            {
                </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Console</span><span style="background:black none repeat scroll 0 0;color:white;">.WriteLine(a.gac.MajorName + </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">" == " </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">+ a.RegVal);
                </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Console</span><span style="background:black none repeat scroll 0 0;color:white;">.WriteLine(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">" ==&#62;" </span></span><span style="font-family:Consolas;"><span style="background:black none repeat scroll 0 0;color:white;">+ a.gac.FullName);
            }

            </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Console</span><span style="background:black none repeat scroll 0 0;color:white;">.WriteLine(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"Search Took : " </span><span style="background:black none repeat scroll 0 0;color:white;">+ sw.ElapsedMilliseconds + </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">" ms"</span></span><span style="background:black none repeat scroll 0 0;color:white;"><span style="font-size:x-small;font-family:Consolas;">);
        }</span>
</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h2>Appendix : A Few Extension Methods we Use<span style="background:black none repeat scroll 0 0;color:white;"> </span></h2>
<pre class="code"><span style="background:black none repeat scroll 0 0;color:white;">  </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">static class </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">LinqProviderTestExtensions
    </span><span style="background:black none repeat scroll 0 0;color:white;">{
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;summary&#62;
        /// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Will perform each action for each
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">'row' in the included collection
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Each 'row' is of Type TTreeNode
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// </span><span style="background:black none repeat scroll 0 0;color:#9ff5fd;">Will typically only be one action
        </span><span style="background:black none repeat scroll 0 0;color:gray;">/// &#60;/summary&#62;
        /// &#60;typeparam name=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"T"</span><span style="background:black none repeat scroll 0 0;color:gray;">&#62;&#60;/typeparam&#62;
        /// &#60;param name=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"items"</span><span style="background:black none repeat scroll 0 0;color:gray;">&#62;&#60;/param&#62;
        /// &#60;param name=</span><span style="background:black none repeat scroll 0 0;color:#9feca5;">"actions"</span><span style="background:black none repeat scroll 0 0;color:gray;">&#62;&#60;/param&#62;
        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">static internal void </span><span style="background:black none repeat scroll 0 0;color:white;">ForEach&#60;T&#62;(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">this </span><span style="background:black none repeat scroll 0 0;color:#9185fa;">IEnumerable</span><span style="background:black none repeat scroll 0 0;color:white;">&#60;T&#62; items, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">params </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Action</span><span style="background:black none repeat scroll 0 0;color:white;">&#60;T&#62;[] actions)
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">foreach </span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">t </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">in </span><span style="background:black none repeat scroll 0 0;color:white;">items)
                </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">foreach </span><span style="background:black none repeat scroll 0 0;color:white;">(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">var </span><span style="background:black none repeat scroll 0 0;color:white;">act </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">in </span><span style="background:black none repeat scroll 0 0;color:white;">actions)
                    act.Invoke(t);
        }

        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public static string </span><span style="background:black none repeat scroll 0 0;color:white;">FirstRegValue(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">this </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">RegistryKey </span><span style="background:black none repeat scroll 0 0;color:white;">registryKey)
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">registryKey.ValueCount &#62; 0
                           ? (</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">string</span><span style="background:black none repeat scroll 0 0;color:white;">) registryKey.GetValueNames().GetValue(0) : </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">String</span><span style="background:black none repeat scroll 0 0;color:white;">.Empty;
        }

        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public static double </span><span style="background:black none repeat scroll 0 0;color:white;">ToPercent(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">this int </span><span style="background:black none repeat scroll 0 0;color:white;">target, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">total)
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">target.ToPercent((</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">double</span><span style="background:black none repeat scroll 0 0;color:white;">) total);
        }

        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public static </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">String </span><span style="background:black none repeat scroll 0 0;color:white;">ToPctString(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">this int </span><span style="background:black none repeat scroll 0 0;color:white;">target, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">int </span><span style="background:black none repeat scroll 0 0;color:white;">total)
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:white;">target.ToPercent((</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">double</span><span style="background:black none repeat scroll 0 0;color:white;">) total) + </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">"%"</span><span style="background:black none repeat scroll 0 0;color:white;">;
        }

        </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">public static double </span><span style="background:black none repeat scroll 0 0;color:white;">ToPercent(</span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">this int </span><span style="background:black none repeat scroll 0 0;color:white;">target, </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">double </span><span style="background:black none repeat scroll 0 0;color:white;">total)
        {
            </span><span style="background:black none repeat scroll 0 0;color:#ecc03e;">return </span><span style="background:black none repeat scroll 0 0;color:#2b91af;">Math</span><span style="background:black none repeat scroll 0 0;color:white;">.Round((target/total)*100, 2);
        }
    }</span></pre>
<pre class="code"><span style="background:black none repeat scroll 0 0;color:white;">A huge Thanks:</span></pre>
<pre class="code"><a title="http://dotnetjunkies.com/WebLog/debasish/" href="http://dotnetjunkies.com/WebLog/debasish/" target="_blank">Debasish Bose</a></pre>
<pre class="code">
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:4996f79e-bdd5-4823-85e8-11fd4c706280" class="wlWriterSmartContent" style="display:inline;float:none;margin:0;padding:0;">del.icio.us Tags: <a rel="tag" href="http://del.icio.us/popular/Advanced+C%23">Advanced C#</a>,<a rel="tag" href="http://del.icio.us/popular/C%23+Unmanaged+Wrapper">C# Unmanaged Wrapper</a>,<a rel="tag" href="http://del.icio.us/popular/Fusion">Fusion</a>,<a rel="tag" href="http://del.icio.us/popular/GAC">GAC</a>,<a rel="tag" href="http://del.icio.us/popular/Global+Assembly+Cache">Global Assembly Cache</a>,<a rel="tag" href="http://del.icio.us/popular/IEnumerable">IEnumerable</a>,<a rel="tag" href="http://del.icio.us/popular/IEnumerator">IEnumerator</a>,<a rel="tag" href="http://del.icio.us/popular/Internals">Internals</a>,<a rel="tag" href="http://del.icio.us/popular/Lambada">Lambada</a>,<a rel="tag" href="http://del.icio.us/popular/Linq+to+Gac">Linq to Gac</a>,<a rel="tag" href="http://del.icio.us/popular/Linq+to+Objects">Linq to Objects</a>,<a rel="tag" href="http://del.icio.us/popular/Machine">Machine</a>,<a rel="tag" href="http://del.icio.us/popular/Registry">Registry</a></div>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:d890b055-0d6b-4680-a4e0-6f7fb6c20546" class="wlWriterSmartContent" style="display:inline;float:none;margin:0;padding:0;">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/Advanced+C%23">Advanced C#</a>,<a rel="tag" href="http://technorati.com/tags/C%23+Unmanaged+Wrapper">C# Unmanaged Wrapper</a>,<a rel="tag" href="http://technorati.com/tags/Fusion">Fusion</a>,<a rel="tag" href="http://technorati.com/tags/GAC">GAC</a>,<a rel="tag" href="http://technorati.com/tags/Global+Assembly+Cache">Global Assembly Cache</a>,<a rel="tag" href="http://technorati.com/tags/IEnumerable">IEnumerable</a>,<a rel="tag" href="http://technorati.com/tags/IEnumerator">IEnumerator</a>,<a rel="tag" href="http://technorati.com/tags/Internals">Internals</a>,<a rel="tag" href="http://technorati.com/tags/Lambada">Lambada</a>,<a rel="tag" href="http://technorati.com/tags/Linq+to+Gac">Linq to Gac</a>,<a rel="tag" href="http://technorati.com/tags/Linq+to+Objects">Linq to Objects</a>,<a rel="tag" href="http://technorati.com/tags/Machine">Machine</a>,<a rel="tag" href="http://technorati.com/tags/Registry">Registry</a></div>
</pre>
<h3>Related Posts</h3>
<div class="wlw_related_posts">from tag <a href="http://del.icio.us/damoncarr/Fusion">Fusion</a></p>
<ul>
<li><a href="http://blog.domaindotnet.com/2008/09/20/fusion_c_sharp_wrapper_for_linq_to_gac_access/">Linq to Gac : Use Linq to Power Query your Gac via this C# Bridge to Fusion « team domain.dot.net</a></li>
</ul>
<p>from tag <a href="http://del.icio.us/damoncarr/Lambada">Lambada</a></p>
<ul>
<li><a href="http://blog.domaindotnet.com/2008/07/24/linq-c-30-enumerable-extension-method-reference/">Linq C# 3.0 Enumerable Extension Method Reference « don’t give up on software just yet</a></li>
</ul>
<p>from tag <a href="http://del.icio.us/damoncarr/Linq">Linq</a></p>
<ul>
<li><a href="http://www.istartedsomething.com/20061029/royale-noir/">Royale Noir: secret XP theme uncovered (download) &#8211; istartedsomething</a></li>
<li><a href="/cifs/opt">linux</a></li>
<li><a href="http://www.google.co.uk/search?hl=en&#38;ie=UTF-8&#38;q=%s&#38;meta=cr%3DcountryUK%7CcountryGB">guk</a></li>
<li><a href="http://msdn2.microsoft.com/en-us/library/ms345135.aspx">Managed Data Access Inside SQL Server with ADO.NET and SQLCLR</a></li>
<li><a href="http://damon.agilefactor.com/feed">where(dev=&#62; Is.Broken(dev)).tryinject(commonsense)</a></li>
<li><a href="http://www.daveandal.net/articles/esbdocs/">Microsoft ESB Guidance (August 2007 CTP Release)</a></li>
<li><a href="http://pinguy.infogami.com/blog/210ee">What to Install After a Fresh XP Install (PinGUY&#8217;s Website)</a></li>
<li><a href="http://evanhoff.com/archive/2007/07/23/39.aspx">NHibernate SysCache Issues &#8211; Solved</a></li>
<li><a href="http://nightweed.com/printableusavotefacts.html">20 Amazing Facts About  Voting in the USA</a></li>
<li><a href="http://cdimage.ubuntu.com/kubuntu-kde4/releases/8.04/release/kubuntu-kde4-8.04.1-desktop-i386.iso">http://cdimage.ubuntu.com/kubuntu-kde4/releases/8.04/release/kubuntu-kde4-8.04.1-desktop-i386.iso</a></li>
<li><a href="http://feeds.feedburner.com/adaptiveblue/feed">BlueBlog</a></li>
<li><a href="http://damoncarr.freepolls.com/cgi-bin/util/site_admin">free Web polls</a></li>
<li><a href="http://code.google.com/">Developer Home &#8211; Google Code</a></li>
<li><a href="http://www.xmllab.net/mvpxml/">Mvp.Xml Library Documentation &#8211; Mvp.Xml.Common Namespace</a></li>
<li><a href="http://www.codeplex.com/esb">patterns &#38; practices Enterprise Service Bus Guidance &#8211; Home</a></li>
<li><a href="http://www.ifcfilms.com/">IFC Entertainment</a></li>
<li><a href="http://www.buildinglink.com/default.asp">BuildingLink</a></li>
<li><a href="http://msdn.microsoft.com/msdnmag/issues/07/02/SQLRegex/default.aspx">SQL Server 2005 Regular Expressions Make Pattern Matching</a></li>
</ul>
<p><a href="http://del.icio.us/damoncarr/Linq">(more..)</a></div>
<div class="wlWriterHeaderFooter" style="text-align:left;margin:0;padding:4px 0;"><a href="http://digg.com/submit?url=http%3a%2f%2fdcarr.wordpress.com%2f2008%2f09%2f20%2ffusion_c_sharp_wrapper_for_linq_to_gac_access%2f&#38;title=Linq+to+Gac+%3a+Use+Linq+to+Power+Query+your+Gac+via+this+C%23+Bridge+to+Fusion"><img style="border:0;" title="Digg This" src="http://digg.com/img/badges/100x20-digg-button.png" border="0" alt="Digg This" width="100" height="20" /></a></div>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Search word or phrase within a file using LINQ]]></title>
<link>http://codingsense.wordpress.com/2008/09/16/search-word-or-phrase-within-a-file/</link>
<pubDate>Tue, 16 Sep 2008 15:19:53 +0000</pubDate>
<dc:creator>codingsense</dc:creator>
<guid>http://codingsense.wordpress.com/2008/09/16/search-word-or-phrase-within-a-file/</guid>
<description><![CDATA[Hi found a nice way to search a word or phrase within a file, so thought of sharing with you all. st]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:x-small;">Hi found a nice way to search a word or phrase within a file, so thought of sharing with you all.</span></p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:x-small;">string</span><span style="font-size:x-small;"> DirPath = </span><span style="color:#2b91af;font-size:x-small;">Application</span><span style="font-size:x-small;">.StartupPath + </span><span style="color:#a31515;font-size:x-small;">"\Search\"</span><span style="font-size:x-small;">;</span>
<span style="color:#0000ff;font-size:x-small;">string</span><span style="font-size:x-small;"> SearchText = </span><span style="color:#a31515;font-size:x-small;">"Temp"</span><span style="font-size:x-small;">;</span>
<span style="color:#0000ff;font-size:x-small;">string</span><span style="font-size:x-small;">[] Files = (</span><span style="color:#0000ff;font-size:x-small;">from</span><span style="font-size:x-small;"> file </span><span style="color:#0000ff;font-size:x-small;">in <span style="color:#0000ff;font-size:x-small;">new </span></span><span style="color:#2b91af;font-size:x-small;">DirectoryInfo</span><span style="font-size:x-small;">(DirPath).GetFiles()</span>
<span style="color:#0000ff;font-size:x-small;">                     where <span style="color:#0000ff;font-size:x-small;">new </span></span><span style="color:#2b91af;font-size:x-small;">StreamReader</span><span style="font-size:x-small;">(file.FullName).ReadToEnd().Contains(SearchText)</span>
<span style="color:#0000ff;font-size:x-small;">                     select</span><span style="font-size:x-small;"> file.Name).ToArray&#60;</span><span style="color:#0000ff;font-size:x-small;">string</span><span style="font-size:x-small;">&#62;();</span></pre>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:x-small;"><br />
<a title="Download Sample" href="http://www.box.net/shared/aq5sivncon">Download Sample &#8211; 21 kb</a></span></p>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:x-small;">If you know a better way, please post a comment.</p>
<p></span></p>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:x-small;">Happy Learning <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </span></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Difference between Select and SelectMany in LINQ]]></title>
<link>http://codingsense.wordpress.com/2008/09/16/differnce-between-select-and-selectmany-in-linq/</link>
<pubDate>Tue, 16 Sep 2008 12:08:52 +0000</pubDate>
<dc:creator>codingsense</dc:creator>
<guid>http://codingsense.wordpress.com/2008/09/16/differnce-between-select-and-selectmany-in-linq/</guid>
<description><![CDATA[The difference between select and selectMany can be seen when we are accessing data from multilevel ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;">The difference between select and selectMany can be seen when we are accessing data from multilevel classes.</span></p>
<p><a title="Download Sample Code - 2.62Kb" href="http://www.box.net/shared/ceh70yyupr" target="_blank"><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;"> </span>Download Sample Code &#8211; 2.62 Kb</a></p>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;">In the below example we shall see what exactly is the difference while accessing the properties.</span></p>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;">When we access some data by using select it gives us the data that is grouped under the parent (i.e gives multiple arrays). To access the output we need to loop twice.</span></p>
<pre style="border:1px dashed #999999;overflow:auto;width:95%;color:#000000;line-height:14px;padding:5px;"><span style="color:#008000;font-size:small;">////This code shows how to use Select()             </span>
<span style="color:#0000ff;font-size:small;">var</span><span style="font-size:small;"> Query2 = Builders.Select(Build =&#62; Build.Locations);</span>

<span style="color:#008000;font-size:small;">// Notice that two foreach loops are required to iterate through
<span style="color:#008000;font-size:small;">// the results because the query returns a collection of arrays.            </span></span>
<span style="color:#0000ff;font-size:small;">foreach</span><span style="font-size:small;"> (</span><span style="color:#2b91af;font-size:small;">List</span><span style="font-size:small;">&#60;</span><span style="color:#2b91af;font-size:small;">String</span><span style="font-size:small;">&#62; BuildList </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> Query2)
{</span>
<span style="color:#0000ff;font-size:small;">        foreach</span><span style="font-size:small;"> (</span><span style="color:#0000ff;font-size:small;">string</span><span style="font-size:small;"> builds </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> BuildList)
        {</span>
                <span style="color:#2b91af;font-size:small;">Console</span><span style="font-size:small;">.WriteLine(builds);
        }</span>

        <span style="color:#2b91af;font-size:small;">Console</span><span style="font-size:small;">.WriteLine();
}</span></pre>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;">But using the same sample when we use select many it gives the selected items in one array(i.e it give the output as it is joining all the output from select command)</span></p>
<pre style="border:1px dashed #999999;overflow:auto;width:95%;color:#000000;line-height:14px;padding:5px;"><span style="color:#008000;font-size:small;">// Query using SelectMany().            </span>
<span style="color:#0000ff;font-size:small;">var</span><span style="font-size:small;"> query1 = Builders.SelectMany(Build =&#62; Build.Locations);</span>

<span style="color:#008000;font-size:small;">// Only one foreach loop is required to iterate through the
<span style="color:#008000;font-size:small;">// results because it is a one-dimensional collection.
<span style="color:#008000;font-size:small;">// Select many joins the output of all the sub collections in to one.            </span></span></span>
<span style="color:#0000ff;font-size:small;">foreach</span><span style="font-size:small;"> (</span><span style="color:#0000ff;font-size:small;">string</span><span style="font-size:small;"> Loc </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> query1)</span>
        <span style="color:#2b91af;font-size:small;">Console</span><span style="font-size:small;">.WriteLine(Loc);</span></pre>
<p><strong>Output:</strong></p>
<div id="attachment_51" class="wp-caption alignnone" style="width: 80px"><a href="http://codingsense.files.wordpress.com/2008/09/selectmany1.jpg"><img class="size-thumbnail wp-image-51" title="Difference select and selectMany" src="http://codingsense.wordpress.com/files/2008/09/selectmany1.jpg?w=70" alt="Difference select and selectMany" width="70" height="96" /></a><p class="wp-caption-text">Difference Select and SelectMany</p></div>
<p>Happy learning <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[LINQ to system files and processes]]></title>
<link>http://codingsense.wordpress.com/2008/09/16/linq-to-system-files-and-processes/</link>
<pubDate>Tue, 16 Sep 2008 05:27:06 +0000</pubDate>
<dc:creator>codingsense</dc:creator>
<guid>http://codingsense.wordpress.com/2008/09/16/linq-to-system-files-and-processes/</guid>
<description><![CDATA[I was just thinking where and all linq can be used, surpirsingly i found that whatever i thought to ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;"><br />
I was just thinking where and all linq can be used, surpirsingly i found that whatever i thought to do i was able to implement linq to make it much better.</span></p>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;">Firstly i thought how linq can be used with file system and check what i got.</span></p>
<p><strong>LINQ to System Files:</strong></p>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;">Here i made a sample which will search the files from system directory and list the files that are of .dll extension and have size greater than 100000 bytes</span></p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:small;">string</span><span style="font-size:small;"> DirPath = System.</span><span style="color:#2b91af;font-size:small;">Environment</span><span style="font-size:small;">.GetFolderPath(</span><span style="color:#2b91af;font-size:small;">Environment</span><span style="font-size:small;">.</span><span style="color:#2b91af;font-size:small;">SpecialFolder</span><span style="font-size:small;">.System);</span>
<span style="color:#0000ff;font-size:small;">string</span><span style="font-size:small;">[] Files = (</span><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> file </span><span style="color:#0000ff;font-size:small;">in <span style="color:#0000ff;font-size:small;">new </span></span><span style="color:#2b91af;font-size:small;">DirectoryInfo</span><span style="font-size:small;">(DirPath).GetFiles()</span>
<span style="color:#0000ff;font-size:small;">                     where</span><span style="font-size:small;"> file.Extension == </span><span style="color:#a31515;font-size:small;">".dll"</span><span style="font-size:small;"> &#38;&#38; file.Length &#62; 1000000</span>
<span style="color:#0000ff;font-size:small;">                     select</span><span style="font-size:small;"> file.Name).ToArray&#60;</span><span style="color:#0000ff;font-size:small;">string</span><span style="font-size:small;">&#62;();</span></pre>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;"><br />
Next i thought how linq can be used with processes </span></p>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;"><strong>LINQ to system Processes:</strong><br />
</span><br />
<span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;">Here we shall see how a running process can be handled using LINQ. This sample will list all the processes that are currently running and order them in ascending order of the memory taken</span></p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><span style="color:#0000ff;font-size:small;">var</span><span style="font-size:small;"> ProcInfo = </span><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> proc </span><span style="color:#0000ff;font-size:small;">in </span><span style="color:#2b91af;font-size:small;">Process</span><span style="font-size:small;">.GetProcesses()</span>
<span style="color:#0000ff;font-size:small;">       orderby</span><span style="font-size:small;"> proc.PagedMemorySize64</span>
<span style="color:#0000ff;font-size:small;">       select <span style="color:#0000ff;font-size:small;">new </span></span><span style="font-size:small;">{ Name = proc.ProcessName , Memory = proc.PagedMemorySize64};</span></pre>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;"><br />
<a title="Download sample" href="http://www.box.net/shared/4df469hjvj">Download sample &#8211; 24 kb</a></span></p>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;">So people just keep thinking where all you can utilize linq and you will get more creatives ways to use it.</span></p>
<p> </p>
<p><span style="font-family:Verdana, Arial, Helvetica, sans-serif;font-size:small;">Happy learing <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </span></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Samples for all Linq functions at one place]]></title>
<link>http://codingsense.wordpress.com/2008/09/11/samples-for-all-linq-functions-at-one-place/</link>
<pubDate>Thu, 11 Sep 2008 15:03:21 +0000</pubDate>
<dc:creator>codingsense</dc:creator>
<guid>http://codingsense.wordpress.com/2008/09/11/samples-for-all-linq-functions-at-one-place/</guid>
<description><![CDATA[I had many confusions when i used linq for the first time. For each function i searched its usage an]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>I had many confusions when i used linq for the first time. For each function i searched its usage and then used it. It was pretty time consuming. So i decided to make a sample that will explain almost all linq functions at one place.</p>
<p>Here comes the sample.<br />
<a href="http://www.box.net/shared/2q9py1y0im">Download sample &#8211; 45.2Kb</a></p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;"><code>int[] arrNumbers1 = { 1, 4, 8, 4, 15, 12, 15};
int[] arrNumbers2 = { 2, 9, 3, 5, 8, 18, 15 };</code>

<code>//********Aggregate**********
//The LINQ aggregate operators allow you to perform simple math
//operations over the elements in a collection in a sequence</code>

<code>//1.Take min of the elements in a collection
int ArrgrNum = arrNumbers2.Aggregate((s, t) =&#62; Math.Min(s, t));

//2.Lets reverse a string
string sentence = "I went with her for a walk in evening";
// Split the string into individual words.
string[] words = sentence.Split(' ');
// Join each word to the beginning of the new sentence to reverse the word oder.
string reverse = words.Aggregate((CurrentWord, next) =&#62; next + " " + CurrentWord);
Console.WriteLine(reverse);

//3.Lets check with an example to find the factorial of a given Number.
int N = 5;
IEnumerable intSequence = Enumerable.Range(1, N);
int agg = intSequence.Aggregate((av, e) =&#62; av * e);
Console.WriteLine("{0}! = {1}", N, agg);

//*********ALL*****************
//All&#60;&#62; does not return a list of the items in the collection.
//It returns a bool indicating if all the items in the list match an expression.

//We will first check whether all NO's in the array are even or not
var AllEven = arrNumbers1.All(temp =&#62; temp % 2 == 0);

//Here we check all the numbers in the array are greater than 1 or not
var AllGreater = arrNumbers2.All(Greater =&#62; Greater &#62; 1);

//********Any*****************
//Any is reverse of All, it checks like "If any element satisfies the condition return true"
var AnyEven = arrNumbers1.Any(temp =&#62; temp % 2 == 0);

//******Average*********
var AvgNum =  arrNumbers1.Average();

//conditional avereage
//This takes the average of all elemets in an array whose value are greater than 8
//If less then it will replace it with 8 and take the average
var AvgOther = arrNumbers1.Average(temp =&#62; temp &#62; 8 ? temp : 8);

//********Cast***********
var strArray =  arrNumbers1.Cast();

//********Concat********
//Concatenates both the array
var ConcatArr = arrNumbers2.Concat(arrNumbers1);

//*********Contains******
bool hasElement = arrNumbers1.Contains(2);

//*********Distinct*********
//Removes the duplicates and returns only distinct elements
int[] DistinctOnly = arrNumbers1.Distinct().ToArray();

//*********ELement at********
//Returns Element at give index
var ElementAt = arrNumbers2.ElementAt(3);

//ElementAtOrDefault
//returns the element if its present at the given index
//Or returns its default eg: int 0, stirng empty
var ElementAtOrDef = arrNumbers2.ElementAtOrDefault(15);

//*********Except eg***********
//Selects the elements other than the specified in parameter
var ExceptEg = arrNumbers2.Except(arrNumbers1);
var ExceptEg1 = arrNumbers1.Except(Enumerable.Range(1,5));

//*********First*********
//Returns the first element that matches the condition
var FirstEle = arrNumbers1.First(temp =&#62; temp &#62; 8);

//********FirstOrDefault******
//Returns the first element that matches the condition or
//returns the default value
var FirstOrDef = arrNumbers1.FirstOrDefault(temp =&#62; temp &#62; 15);

//******GetValue*********
//Gets value of the element at given Index
var GetValuesAtIndex = arrNumbers1.GetValue(3);

//*********Intersect**********
//Returns the elements those are comman in both the arrays
var IntersectOfBoth = arrNumbers1.Intersect(arrNumbers2);

//********Last***********
//Gives the last element matching the condition specified in the parameter
var Last = arrNumbers2.Last(temp =&#62; temp &#62; 6);

//*********LastOrdefault********
//Gives the last element matching the condition specified in the parameter
//If not found then returns the default value
var LastOrDef = arrNumbers2.LastOrDefault(temp =&#62; temp &#62; 150);

//***********MAX**********
//Returns the max element in the collection
var MaxEle = arrNumbers2.Max();

//**********MIN***********
//Returns the min element in the collection
var MinEle = arrNumbers2.Min();

//********OrderBy**********
//Orders the list in ascending order
var OrderBY = arrNumbers2.OrderBy(x =&#62; x);

//****OrderByDescending******
//ORders the list in Descendig order
var DscArray = arrNumbers2.OrderByDescending(x =&#62; x);

//********Reverse**********
//Reverses the list
var ReverseArr = arrNumbers2.Reverse();

//********Select***********
//Selects the element that satisfies the given condition
//here we will select all elements as power of 2
var SelectArr = arrNumbers2.Select(ele =&#62; Math.Pow(2,ele));

//*********SequenceEqual******
//If both the arrays are equal return true else return false
bool AreEqual = arrNumbers2.SequenceEqual(arrNumbers1);

//********Set Value**********
//Sets the given value to the element in the collection at given index
arrNumbers1.SetValue(100, 2);

//**********Single*************
//Returns the index of the element whose value is equal to the given parameter
//If it has 0 or 2 elements with same value then it gives an error.
int SingleIndex = arrNumbers1.Single(imp =&#62; imp == 1);

//*********SingleDefault**********
//Returns the index of the element whose value is equal to the given parameter
//If no element with the given value then it returns the default
int SingleDef = arrNumbers1.SingleOrDefault(imp =&#62; imp == 10);

//**********Skip*********
//Skip first 3 elements and returs other elements
var SkipFirstElements = arrNumbers2.Skip(3);

//*********SkipWhile********
//Here in this sample we will extract only those no that are
//divisible by 3
var SkipConditional = arrNumbers1.SkipWhile(num =&#62; num % 3 != 0);

//********Sum***********
//Get the sum of an array
var SumOfArray = arrNumbers1.Sum();

//*********Take*********
//Its just the reverse operation of skip
//here it takes the first n elements specified in the parameter
var Take = arrNumbers2.Take(3);

//*********TakeWhile*********
//Takes only those elements that are not divisible by 3
var TakeWhile = arrNumbers2.TakeWhile(num =&#62; num % 3 != 0);

//*********ToArray***********
//converts the IEnumerable ouput an array of type specified in T
int[] IntArray = arrNumbers1.Take(3).ToArray();

//********Union***********
//Union of both the arrays - only unique
int[] UnionOfBoth = arrNumbers1.Union(arrNumbers2).ToArray();

//**********Where***********
//Select elements those are greater than 5
int[] GreaterThan5 = arrNumbers2.Where(x =&#62; x &#62; 5).ToArray();

</code></pre>
<p>Any suggestions are wide open.</p>
<p>Happy learning <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[LINQ with best example - 1]]></title>
<link>http://codingsense.wordpress.com/2008/09/08/better-linq-with-better-example-1/</link>
<pubDate>Mon, 08 Sep 2008 13:10:09 +0000</pubDate>
<dc:creator>codingsense</dc:creator>
<guid>http://codingsense.wordpress.com/2008/09/08/better-linq-with-better-example-1/</guid>
<description><![CDATA[What is LINQ?? LINQ stand for Language-Integrated Query is now available as a integral part of Visua]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><strong>What is LINQ??</strong></p>
<p class="MsoNormal"><strong><span style="font-weight:normal;">LINQ stand for Language-Integrated Query is now available as a integral part of Visual Studio 2008.</span></strong></p>
<p class="MsoNormal">LINQ has a great power of querying on any source of data, data source could be the collections of objects, database or XML files. We can easily retrieve data from any object that implements the IEnumerable&#60;T&#62; interface. Microsoft basically divides LINQ into three areas and that are give below.</p>
<ul type="disc">
<li class="MsoNormal"><span>LINQ to Object </span></li>
<li class="MsoNormal"><span>LINQ to ADO.Net</span>
<ul type="circle">
<li class="MsoNormal"><span>LINQ to SQL </span></li>
<li class="MsoNormal"><span>LINQ to Dataset </span></li>
<li class="MsoNormal"><span>LINQ to Entities </span></li>
</ul>
</li>
<li class="MsoNormal"><span>LINQ to XML </span></li>
</ul>
<div>
<p class="MsoNormal"><strong>Basics samples of LINQ:</strong></p>
<p class="MsoNormal"><a href="http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx"><span>http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx</span></a></p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">Assuming you know the basics of LINQ I will continue with a bit high end sample lets move further with an example of a company.</p>
<p class="MsoNormal"> </p>
<h1 style="text-align:center;"><strong><span><span style="text-decoration:underline;">The Company Sample</span></span></strong></h1>
<p class="MsoNormal" style="text-align:left;"><strong>Downloads:</strong></p>
<p class="MsoNormal" style="text-align:left;"><a href="http://www.box.net/shared/ts4u2oqzpp">Download complete Project (56.66kb)</a></p>
<p class="MsoNormal">Here we will see how a linq can be utilized in a company module. The company class will contain a Collection of employee class and employee will contain a struct for individual addresses.</p>
<p class="MsoNormal"> <strong>Hierarchy:</strong></p>
<div id="attachment_13" class="wp-caption alignnone" style="width: 310px"><a href="http://codingsense.files.wordpress.com/2008/09/companylinq.jpg"><img class="size-medium wp-image-13" title="CompanyHierarchyLinq" src="http://codingsense.wordpress.com/files/2008/09/companylinq.jpg?w=300" alt="CompanyHierarchyLinq" width="300" height="242" /></a><p class="wp-caption-text">Company Hierarchy</p></div>
<p>If we have a project with above hierarchy, let&#8217;s see how we can use LINQ to access details from multilevel classes. After creation of companies we will try to find the details at multilevel and see how conditionally data can be fetched.<br />
1)  List details of employees<br />
2)  Count no of employees in each company<br />
3)  List employees who are staying in Bangalore<br />
4)  List employee who is paid highest in each companies<br />
5)  Overall Highest paid employee<br />
6)  Salary paid by companies in each city<br />
7)  Salary paid by each company in each city.</p>
<p><strong>List details of employees:</strong><br />
Now we shall list all the employees with their details and company name.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<pre><span style="font-size:small;"><span style="color:#0000ff;">Var </span></span><span style="font-size:small;">EmpDetails = </span><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> comp </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> ListCompany</span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">select</span><span style="font-size:small;"> </span><span style="color:#0000ff;font-size:small;">new</span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;"><span style="color:#000000;font-size:13px;">{</span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;"><span style="color:#000000;font-size:13px;"><span style="font-size:small;">Emp = (</span><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> emp </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> comp.ListEmp</span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;"><span style="color:#000000;font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">select</span><span style="font-size:small;"> </span><span style="color:#0000ff;font-size:small;">new</span><span style="font-size:small;"> { Company = comp.Name, emp })</span></span></span></span></span></span></span>
};</pre>
</pre>
<p>Here we use sub queries since we need all employee details with their respective company name and both the details are at different levels. So first we Loops through the companies in the company list from comp in ListCompany and then within each selected company it loops through each employee from emp in comp.ListEmp <strong>Count no of employees in each company:</strong></p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<pre><span style="color:#0000ff;font-size:small;">var</span><span style="font-size:small;"> LessEmp = </span><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> Comp </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> ListCompany </span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">select</span><span style="font-size:small;"> <span style="color:#0000ff;font-size:13px;">new<span style="color:#000000;">{Comp.Name, EmpCount = Comp.ListEmp.Count };</span></span></span></span></span></pre>
</pre>
<p><strong>List employees who are staying in Bangalore:</strong> Here we use <strong>compound from clause</strong> to retrieve the employees who are staying in any city that contains BAN or ban.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<pre><span style="color:#0000ff;font-size:small;">var</span><span style="font-size:small;"> EmpInACity = </span><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> comp </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> ListCompany <span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> emplist </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> comp.ListEmp </span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">where</span><span style="font-size:small;"> emplist.Address.City.ToUpper().Contains(</span><span style="color:#a31515;font-size:small;">"BAN"</span><span style="font-size:small;">) </span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">select</span><span style="font-size:small;"> </span><span style="color:#0000ff;font-size:small;">new</span><span style="font-size:small;"> { <span style="font-size:13px;">CompName = comp.Name, EmployeeName = emplist.Name };</span></span></span></span></span></span></span></span></pre>
</pre>
<p><strong>List employee who is paid highest in each companies:</strong> For finding the highest paid employee we first loop through all the companies and employees in each company using compound from clause. Where condition filters only those employees those have the salary equal to the max salary in current company.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<pre><span style="color:#0000ff;font-size:small;">Var</span><span style="font-size:small;"> EmpHighSal = <span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> comp </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> ListCompany </span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;">from</span><span style="font-size:small;"> empHigh </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> comp.ListEmp </span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">where</span><span style="font-size:small;"> empHigh.salary == comp.ListEmp.Max(HighEmp =&#62; HighEmp.salary) </span></span></span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">select</span><span style="font-size:small;"> </span><span style="color:#0000ff;font-size:small;">new</span><span style="font-size:small;"> { <span style="font-size:13px;">CompanyName = comp.Name, EmpHighName = empHigh.Name, </span></span></span></span></span></span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;">EmpHighSal = empHigh.salary <span style="font-size:small;">};</span> </span></span></span></span></span></span></span></span></span></span></pre>
</pre>
<p><strong>Overall Highest paid employee:</strong> The same above procedure is followed but the only difference comes in the where condition where we match the employee&#8217;s salary to the max salary from all the companies.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;height:125px;border:#999999 1px dashed;padding:5px;">
<pre><span style="color:#0000ff;font-size:small;">var</span><span style="font-size:small;"> EmpHighSal = </span><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> comp </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> ListCompany</span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> emp </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> comp.ListEmp <span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">where</span><span style="font-size:small;"> emp.salary == </span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;">ListCompany.Max(TComp =&#62; TComp.ListEmp.Max(HighEmp =&#62; HighEmp.salary))</span></span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">select</span><span style="font-size:small;"> </span><span style="color:#0000ff;font-size:small;">new</span><span style="font-size:small;"> { <span style="font-size:13px;">CompanyName = comp.Name , EmployeeName = emp.Name, </span></span></span></span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;">EmpSal = emp.salary };</span></span></span></span></span></span></span></span></pre>
</pre>
<p><strong>Salary paid by companies in each city:</strong> Here we will group by city and sum up all the salaries of the employees who are staying in that city.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;height:93px;border:#999999 1px dashed;padding:5px;">
<pre><span style="color:#0000ff;font-size:small;">var</span><span style="font-size:small;"> CompanyCityWise = </span><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> comp </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> ListCompany </span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> emp </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> comp.ListEmp <span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">group</span><span style="font-size:small;"> emp </span><span style="color:#0000ff;font-size:small;">by</span><span style="font-size:small;"> emp.Address.City </span><span style="color:#0000ff;font-size:small;">into</span><span style="font-size:small;"> CityWiseEmp </span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">select</span><span style="font-size:small;"> </span><span style="color:#0000ff;font-size:small;">new</span><span style="font-size:small;"> { <span style="font-size:13px;">State = CityWiseEmp.Key, </span></span></span></span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;">TotalSalary = CityWiseEmp.Sum(emp =&#62; emp.salary)};</span></span></span></span></span></span></span></span></pre>
<p> </pre>
<p><strong>Salary paid by each company in each city.</strong> Here for each company a group by clause is applied and from each company employees staying at different locations are fetched.</p>
<pre style="overflow:auto;width:95%;color:#000000;line-height:14px;border:#999999 1px dashed;padding:5px;">
<pre><span style="color:#0000ff;font-size:small;">var</span><span style="font-size:small;"> CityWiseSalary = </span><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> comp </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> ListCompany</span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">select</span><span style="font-size:small;"> </span><span style="color:#0000ff;font-size:small;">new</span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;"><span style="color:#000000;font-size:13px;">{comp.Name, <span style="font-size:small;">Emp =(</span><span style="color:#0000ff;font-size:small;">from</span><span style="font-size:small;"> emp </span><span style="color:#0000ff;font-size:small;">in</span><span style="font-size:small;"> comp.ListEmp </span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;"><span style="color:#000000;font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">group</span><span style="font-size:small;"> emp </span><span style="color:#0000ff;font-size:small;">by</span><span style="font-size:small;"> emp.Address.City </span><span style="color:#0000ff;font-size:small;">into</span><span style="font-size:small;"> CityWiseEmp </span></span></span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;"><span style="color:#000000;font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;">select</span><span style="font-size:small;"> </span><span style="color:#0000ff;font-size:small;">new</span><span style="font-size:small;"> { <span style="font-size:13px;">State = CityWiseEmp.Key, </span></span></span></span></span></span></span></span></span></span>
<span style="font-size:small;"><span style="font-size:13px;"><span style="color:#0000ff;font-size:small;"><span style="color:#000000;font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;"><span style="font-size:small;"><span style="font-size:13px;">TotalSalary = CityWiseEmp.Sum(emp =&#62; emp.salary) })};</span></span></span></span></span></span></span></span></span></span></pre>
</pre>
<p> <br />
<a href="http://codingsense.wordpress.com/2009/01/12/linq-with-best-example-2/">Next &#62;&#62; LINQ with best example &#8211; 2</a></p>
<p><strong>More references on LINQ:</strong> Standard Query Operators in a Nutshell: <a href="http://msdn.microsoft.com/en-us/library/bb308959.aspx#linqoverview_topic8">http://msdn.microsoft.com/en-us/library/bb308959.aspx#linqoverview_topic8</a> More LINQ: <a href="http://msdn.microsoft.com/en-us/library/bb397676.aspx">http://msdn.microsoft.com/en-us/library/bb397676.aspx</a></div>
<div>Suggestions are most welcome. </div>
<div>Happy learning <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </div>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Master Generic Type Constrains : A critical success factor in Linq Development]]></title>
<link>http://team.pushbomb.com/2008/06/27/master-generic-type-constrains-a-critical-success-factor-in-linq-development/</link>
<pubDate>Fri, 27 Jun 2008 17:07:06 +0000</pubDate>
<dc:creator>Damon Wilder Carr</dc:creator>
<guid>http://team.pushbomb.com/2008/06/27/master-generic-type-constrains-a-critical-success-factor-in-linq-development/</guid>
<description><![CDATA[  More accurately called ..&#8216;Variance and Generalized Constraints for C# Generics&#8217;) (1) A]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p> </p>
<p>More accurately called ..<a href="http://research.microsoft.com/~akenn/generics/ECOOP06.pdf">&#8216;Variance and Generalized Constraints for C# Generics&#8217;)</a> (1)</p>
<p><strong>An item we often see teams ‘leaving money on the table’ with is a failure to leverage (or leverage fully) generic type constraints. Now with Linq you cannot get away with it anymore as for now, although far more powerful, there are still long lines of complex generic delegate types to navigate.</strong></p>
<p>This post based on the docs from MSDN, however we’ve added some significant teeth we believe (with more to come) We at least made this potentially more ‘real world’ in complexity.</p>
<div id="ctl00_LibFrame_ra1" class="resizableArea">
<div id="ctl00_LibFrame_raRight" class="rightSection">
<div id="mainSection">
<div id="mainBody">
<div class="topic">
<div id="mainSection">
<div id="mainBody">
<p class="introduction">When you define a generic class, you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates your class. If client code tries to instantiate your class by using a type that is not allowed by a constraint, the result is a compile-time error. These restrictions are called constraints. Constraints are specified by using the <span class="keyword">where</span> contextual keyword. (NOTE: the constraint is LITERAL unless specified otherwise)</p>
<h1>where T: struct    (literal)</h1>
<blockquote><p>The type argument must be a value type. Any value type except <a href="http://msdn.microsoft.com/en-us/library/system.nullable.aspx">Nullable</a> can be specified.</p>
<p>See <a href="http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx">Using Nullable Types (C# Programming Guide)</a> for more information.</p></blockquote>
<h1>where T : class   (literal)</h1>
<blockquote><p>The type argument must be a reference type; this applies also to any class, interface, delegate, or array type.</p></blockquote>
<h1>where T : new()   (literal)</h1>
<blockquote><p>The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last.</p></blockquote>
<h1>where T : SomeClassName   (non-literal)</h1>
<blockquote><p>Example:</p>
<p><span style="background:#100d0c;color:#8ac5ff;">public class Stream</span><span style="background:#100d0c;color:#64b1ff;">List</span><span style="background:#100d0c;color:#c0c0c0;">&#60;</span><span style="background:#100d0c;color:#ffffff;">T</span><span style="background:#100d0c;color:#c0c0c0;">&#62; </span><span style="background:#100d0c;color:#ffffff;">: </span><span style="background:#100d0c;color:#64b1ff;">List</span><span style="background:#100d0c;color:#c0c0c0;">&#60;</span><span style="background:#100d0c;color:#ffffff;">T</span><span style="background:#100d0c;color:#c0c0c0;">&#62; </span><span style="background:#100d0c;color:#8ac5ff;">where </span><span style="background:#100d0c;color:#ffffff;">T : </span><span style="background:#100d0c;color:#8ac5ff;">Stream</span><a href="http://11011.net/software/vspaste"></a></p>
<p>Here we are creating a custom collection from List&#60;T&#62; and also ensuring (see above) that values are reference types.</p></blockquote>
<h1>where T : someinterface    (non-literal)</h1>
<blockquote><p>The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic.</p></blockquote>
<h1>where T : U       (non-literal)</h1>
<blockquote><p>The type argument supplied for T must be or derive from the argument supplied for U. This is called a naked type constraint.</p></blockquote>
<h2>domain.dot.net addition</h2>
<p><span style="font-size:small;color:#ffffff;">Here is an example of a pattern we use that combines the concept of generic constraints to make Extension methods far more effective.</span></p>
<p><span style="font-size:small;color:#ffffff;">Assume you want to allow any Enum to support seamless conversion to a Linq sequence an IEnumerable&#60;TType&#62; where TType : Enum</span></p>
<p><span style="font-size:small;color:#ffffff;">Wait! You cannot do that! Ugh…Enum is one of the invalid constraint types (as is delegate, Array, etc.)</span></p>
<p><span style="font-size:small;color:#ffffff;">Here is how we ‘solve’ this:</span></p>
<p><span style="font-size:small;color:#ffffff;">1) Assert as much as you can about a type</span></p>
<p><span style="font-size:small;color:#ffffff;">2) Do run-time checks if needed</span></p>
<p><span style="font-size:small;color:#ffffff;">Here is the header:</span></p>
<p align="left"><span style="background:#16140f;color:#22bdff;">public static </span><span style="background:#16140f;color:#dde074;">IEnumerable</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#eddfc0;">TEnumType</span><span style="background:#16140f;color:#c0c0c0;">&#62; </span><span style="background:#16140f;color:#eddfc0;">EnumToLinq</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#eddfc0;">TEnumType</span><span style="background:#16140f;color:#c0c0c0;">&#62;</span><span style="background:#16140f;color:#e7696c;">( <br />
</span><span style="background:#16140f;color:#22bdff;">                this </span><span style="background:#16140f;color:#eddfc0;">TEnumType target</span><span style="background:#16140f;color:#e7696c;">)</p>
<p>                     </span><span style="background:#16140f;color:#22bdff;">where </span><span style="background:#16140f;color:#eddfc0;">TEnumType </span><span style="background:#16140f;color:#e7696c;">: </span><span style="background:#16140f;color:#22bdff;">struct</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#dde074;">IComparable</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#dde074;">IFormattable</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#dde074;">IConvertible</span><a href="http://11011.net/software/vspaste"></a></p>
<p><span style="font-size:small;color:#ffffff;">If that immediately makes you feel sick, well this is what refactored code looks like where you try to not force an entire team to work daily here, but it’s unavoidable. After having our heads in this for a while you absolutely need to master this to be a senior Linq developer (and looking at others code is always fantastic for us as we find things we learn we were doing wrong).</span></p>
<p><span style="font-size:small;color:#ffffff;">And here is the full extension method (go ahead and use it for whatever you want if you find value):</span></p>
<p><span style="background:#16140f;color:#22bdff;">public static </span><span style="background:#16140f;color:#dde074;">IEnumerable</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#eddfc0;">TEnumType</span><span style="background:#16140f;color:#c0c0c0;">&#62; </span><span style="background:#16140f;color:#eddfc0;">EnumToLinq</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#eddfc0;">TEnumType</span><span style="background:#16140f;color:#c0c0c0;">&#62;</span><span style="background:#16140f;color:#e7696c;">(</p>
<p>                 </span><span style="background:#16140f;color:#22bdff;">this </span><span style="background:#16140f;color:#eddfc0;">TEnumType target</span><span style="background:#16140f;color:#e7696c;">)</p>
<p>                            </span><span style="background:#16140f;color:#22bdff;">where </span><span style="background:#16140f;color:#eddfc0;">TEnumType </span><span style="background:#16140f;color:#e7696c;">: </span><span style="background:#16140f;color:#22bdff;">struct</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#dde074;">IComparable</span><span style="background:#16140f;color:#e7696c;">,</p>
<p>                                  </span><span style="background:#16140f;color:#dde074;">IFormattable</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#dde074;">IConvertible </span><span style="background:#16140f;color:#e7696c;">{</p>
<p>                                  </span><span style="background:#16140f;color:#22bdff;">var </span><span style="background:#16140f;color:#eddfc0;">_type </span><span style="background:#16140f;color:#c0c0c0;">= </span><span style="background:#16140f;color:#eddfc0;">target</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">GetType</span><span style="background:#16140f;color:#e7696c;">();</p>
<p>                                   i</span><span style="background:#16140f;color:#22bdff;">if </span><span style="background:#16140f;color:#e7696c;">(</span><span style="background:#16140f;color:#c0c0c0;">!</span><span style="background:#16140f;color:#eddfc0;">_type</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">BaseType</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">Equals</span><span style="background:#16140f;color:#e7696c;">(</span><span style="background:#16140f;color:#22bdff;">typeof </span><span style="background:#16140f;color:#e7696c;">(</span><span style="background:#16140f;color:#e7d97a;">Enum</span><span style="background:#16140f;color:#e7696c;">)))</p>
<p>                                        </span><span style="background:#16140f;color:#22bdff;">throw new </span><span style="background:#16140f;color:#e7d97a;">TypeInitializationException</span><span style="background:#16140f;color:#e7696c;">(</p>
<p>                                        </span><span style="background:#16140f;color:#eddfc0;">_type</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">FullName</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#22bdff;">new </span><span style="background:#16140f;color:#e7d97a;">InvalidOperationException</span><span style="background:#16140f;color:#e7696c;">(</p>
<p>                                           </span><span style="background:#16140f;color:#eddfc0;">_type</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">Name </span><span style="background:#16140f;color:#c0c0c0;">+ </span><span style="background:#16140f;color:#a6f0e6;">&#8221; is not an Enum&#8221;</span></p>
<div><span style="background:#16140f;color:#e7696c;">))</p>
<p>                                                                              ; </span></div>
<p><span style="background:#16140f;color:#e7696c;">                  <span style="background:#16140f;color:#22bdff;">return </span><span style="background:#16140f;color:#e7d97a;">Enum</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">GetValues</span><span style="background:#16140f;color:#e7696c;">(</span><span style="background:#16140f;color:#eddfc0;">_type</span><span style="background:#16140f;color:#e7696c;">)</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">Cast</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#eddfc0;">TEnumType</span><span style="background:#16140f;color:#c0c0c0;">&#62;</span><span style="background:#16140f;color:#e7696c;">(); } </span><a href="http://11011.net/software/vspaste"></a></p>
<p></span>Bottom line is we get a nice sequence we can use any Linq to Object expression on.</p>
<p>Here is an example of the above in use:</p>
<p><span style="background:#16140f;color:#adadad;">/// &#60;summary&#62;</p>
<p>/// </span><span style="background:#16140f;color:#7cfc00;">Using a simple bitwise this for now returns</p>
<p></span><span style="background:#16140f;color:#adadad;">/// </span><span style="background:#16140f;color:#7cfc00;">a predicate for later execution and resolution</p>
<p></span><span style="background:#16140f;color:#adadad;">/// </span><span style="background:#16140f;color:#7cfc00;">of a FilleystemInfo. This is used now in Linq</p>
<p></span><span style="background:#16140f;color:#adadad;">/// </span><span style="background:#16140f;color:#7cfc00;">queries to get back various types of things</p>
<p></span><span style="background:#16140f;color:#adadad;">/// </span><span style="background:#16140f;color:#7cfc00;">on the hard disk.</p>
<p></span><span style="background:#16140f;color:#adadad;">/// &#60;/summary&#62;</p>
<p>/// &#60;value&#62;</span><span style="background:#16140f;color:#7cfc00;">To predicate.</span><span style="background:#16140f;color:#adadad;">&#60;/value&#62;</p>
<p>/// </span></p>
<div><span style="background:#16140f;color:#7cfc00;">Documentation Created 6/27/2008 </span></div>
<p><span style="background:#16140f;color:#7cfc00;"><span style="background:#16140f;color:#22bdff;">private static </span><span style="background:black;color:#e1e100;">Predicate</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#e7d97a;">FileSystemInfo</span><span style="background:#16140f;color:#c0c0c0;">&#62; </span><span style="background:#16140f;color:#eddfc0;">ToPredicate </span><span style="background:#16140f;color:#e7696c;">{</p>
<p></span><span style="background:#16140f;color:#22bdff;">get </span><span style="background:#16140f;color:#e7696c;">{</p>
<p></span></p>
<p></span><span style="background:#16140f;color:#7cfc00;">                          // NOTE: This sets the <br />
                      </span></p>
<div><span style="background:#16140f;color:#7cfc00;">    // class value (we only need 1 static)</p>
<p>                          // as well as uses a simple bitwise <br />
                          // &#38; to check if the Directory </span></div>
<p><span style="background:#16140f;color:#7cfc00;">                          // attribute is set</p>
<p>                          <span style="background:#16140f;color:#22bdff;">return </span><span style="background:#16140f;color:#eddfc0;">_persistendPred </span><span style="background:#16140f;color:#c0c0c0;">=</p>
<p></span><span style="background:#16140f;color:#eddfc0;">                                  fileInfo </span><span style="background:#16140f;color:#c0c0c0;">=&#62; </span><span style="background:#16140f;color:#e7696c;">((</span><span style="background:#16140f;color:#eddfc0;">fileInfo</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">Attributes </span><span style="background:#16140f;color:#c0c0c0;">&#38; <br />
                                    </span><span style="background:#16140f;color:#c0c0c0;">   </span><span style="background:#16140f;color:#7cedea;">FileAttributes</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">Directory</span><span style="background:#16140f;color:#e7696c;">) </span><span style="background:#16140f;color:#c0c0c0;">==</p>
<p>                                               </span><span style="background:#16140f;color:#7cedea;">FileAttributes</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">Directory</span><span style="background:#16140f;color:#e7696c;">); }</p>
<p>                               </span><span style="background:#16140f;color:#e7696c;">}</p>
<p></span>Back to MSDN…..</p>
<p></span></p>
<h2><strong></strong></h2>
<h2><strong></strong></h2>
<h2><strong></strong></h2>
<h2><strong></strong></h2>
<h2><strong>Why Use Constraints</strong></h2>
<div id="ctl00_LibFrame_MainContent_cpe468231_c" class="MTPS_CollapsibleSection" style="display:block;overflow:visible;width:auto;height:auto;">
<div class="MTPS_CollapsibleSection" style="border-right:medium none;border-top:medium none;display:block;border-left:medium none;border-bottom:medium none;"><a name="sectionToggle0"></a></div>
<p>If you want to examine an item in a generic list to determine whether it is valid or to compare it to some other item, the compiler must have some guarantee that the operator or method it has to call will be supported by any type argument that might be specified by client code. This guarantee is obtained by applying one or more constraints to your generic class definition. For example, the base class constraint tells the compiler that only objects of this type or derived from this type will be used as type arguments. Once the compiler has this guarantee, it can allow methods of that type to be called in the generic class. Constraints are applied by using the contextual keyword <span class="keyword">where</span>. The following code example demonstrates the functionality we can add to the <span class="code">GenericList&#60;T&#62;</span> class (in <a id="ctl00_LibFrame_MainContent_ctl10" href="http://msdn.microsoft.com/en-us/library/0x6a29h6.aspx">Introduction to Generics (C# Programming Guide)</a>) by applying a base class constraint.</p>
<div>
<div id="ctl00_LibFrame_MainContent_ctl11_CSharp" class="libCScode">
<div class="CodeSnippetTitleBar">
<div class="CodeDisplayLanguage"> </div>
</div>
</div>
</div>
</div>
<p><span style="background:#100d0c;color:#8ac5ff;">public class </span><span style="background:#100d0c;color:#64b1ff;">Employee </span><span style="background:#100d0c;color:#ffffff;">{ </span><span style="background:#100d0c;color:#8ac5ff;">public </span><span style="background:#100d0c;color:#ffffff;">Employee(</span><span style="background:#100d0c;color:#8ac5ff;">string </span><span style="background:#100d0c;color:#ffffff;">s, </span><span style="background:#100d0c;color:#8ac5ff;">int </span><span style="background:#100d0c;color:#ffffff;">i) { Name </span><span style="background:#100d0c;color:#c0c0c0;">= </span><span style="background:#100d0c;color:#ffffff;">s; ID </span><span style="background:#100d0c;color:#c0c0c0;">= </span><span style="background:#100d0c;color:#ffffff;">i; } </span><span style="background:#100d0c;color:#8ac5ff;">public string </span><span style="background:#100d0c;color:#ffffff;">Name { </span><span style="background:#100d0c;color:#8ac5ff;">get</span><span style="background:#100d0c;color:#ffffff;">; </span><span style="background:#100d0c;color:#8ac5ff;">set</span><span style="background:#100d0c;color:#ffffff;">; } </span><span style="background:#100d0c;color:#8ac5ff;">public int </span><span style="background:#100d0c;color:#ffffff;">ID { </span><span style="background:#100d0c;color:#8ac5ff;">get</span><span style="background:#100d0c;color:#ffffff;">; </span><span style="background:#100d0c;color:#8ac5ff;">set</span><span style="background:#100d0c;color:#ffffff;">; } }</span><a href="http://11011.net/software/vspaste"></a></p>
<div></div>
<p><span style="background:#16140f;color:#e7696c;"><span style="background:#16140f;color:#22bdff;">     </span></p>
<div></div>
<p></span><span style="background:#16140f;color:#e7696c;"> </p>
<p></span><span style="background:#100d0c;color:#8ac5ff;">public class </span><span style="background:#100d0c;color:#64b1ff;">GenericList</span><span style="background:#100d0c;color:#c0c0c0;">&#60;</span><span style="background:#100d0c;color:#ffffff;">T</span><span style="background:#100d0c;color:#c0c0c0;">&#62; </span><span style="background:#100d0c;color:#8ac5ff;">where </span><span style="background:#100d0c;color:#ffffff;">T : </span><span style="background:#100d0c;color:#64b1ff;">Employee </span><span style="background:#100d0c;color:#ffffff;">{ </span><span style="background:#100d0c;color:#8ac5ff;">private </span><span style="background:#100d0c;color:#64b1ff;">Node </span><span style="background:#100d0c;color:#ffffff;">head; </span><span style="background:#100d0c;color:#8ac5ff;">public </span><span style="background:#100d0c;color:#ffffff;">GenericList() { head </span><span style="background:#100d0c;color:#c0c0c0;">= </span><span style="background:#100d0c;color:#8ac5ff;">null</span><span style="background:#100d0c;color:#ffffff;">; } </span><span style="background:#100d0c;color:#8ac5ff;">public void </span><span style="background:#100d0c;color:#ffffff;">AddHead(T t) { </span><span style="background:#100d0c;color:#8ac5ff;">var </span><span style="background:#100d0c;color:#ffffff;">n </span><span style="background:#100d0c;color:#c0c0c0;">= </span><span style="background:#100d0c;color:#8ac5ff;">new </span><span style="background:#100d0c;color:#64b1ff;">Node</span><span style="background:#100d0c;color:#ffffff;">(t) { Next </span><span style="background:#100d0c;color:#c0c0c0;">= </span><span style="background:#100d0c;color:#ffffff;">head }; head </span><span style="background:#100d0c;color:#c0c0c0;">= </span><span style="background:#100d0c;color:#ffffff;">n; } </span><span style="background:#100d0c;color:#8ac5ff;">private class </span><span style="background:#100d0c;color:#64b1ff;">Node </span><span style="background:#100d0c;color:#ffffff;">{ </span><span style="background:#100d0c;color:#8ac5ff;">public </span><span style="background:#100d0c;color:#ffffff;">Node(T t) { Next </span><span style="background:#100d0c;color:#c0c0c0;">= </span><span style="background:#100d0c;color:#8ac5ff;">null</span><span style="background:#100d0c;color:#ffffff;">; Data </span><span style="background:#100d0c;color:#c0c0c0;">= </span><span style="background:#100d0c;color:#ffffff;">t; } </span><span style="background:#100d0c;color:#8ac5ff;">public </span><span style="background:#100d0c;color:#64b1ff;">Node </span><span style="background:#100d0c;color:#ffffff;">Next { </span><span style="background:#100d0c;color:#8ac5ff;">get</span><span style="background:#100d0c;color:#ffffff;">; </span><span style="background:#100d0c;color:#8ac5ff;">set</span><span style="background:#100d0c;color:#ffffff;">; } </span><span style="background:#100d0c;color:#8ac5ff;">public </span><span style="background:#100d0c;color:#ffffff;">T Data { </span><span style="background:#100d0c;color:#8ac5ff;">get</span><span style="background:#100d0c;color:#ffffff;">; </span><span style="background:#100d0c;color:#8ac5ff;">set</span><span style="background:#100d0c;color:#ffffff;">; } } </span><a href="http://11011.net/software/vspaste"></a></p>
<p><span style="background:#100d0c;color:#8ac5ff;">blic </span><span style="background:#100d0c;color:#ff6fb7;">IEnumerator</span><span style="background:#100d0c;color:#c0c0c0;">&#60;</span><span style="background:#100d0c;color:#ffffff;">T</span><span style="background:#100d0c;color:#c0c0c0;">&#62; </span><span style="background:#100d0c;color:#ffffff;">GetEnumerator() </span><span style="background:#100d0c;color:#8ac5ff;">public </span><span style="background:#100d0c;color:#ffffff;">T FindFirstOccurrence(</span><span style="background:#100d0c;color:#8ac5ff;">string </span><span style="background:#100d0c;color:#ffffff;">s) { </span><span style="background:#100d0c;color:#8ac5ff;">var </span><span style="background:#100d0c;color:#ffffff;">current </span><span style="background:#100d0c;color:#c0c0c0;">= </span><span style="background:#100d0c;color:#ffffff;">head; T t </span><span style="background:#100d0c;color:#c0c0c0;">= </span><span style="background:#100d0c;color:#8ac5ff;">null</span><span style="background:#100d0c;color:#ffffff;">; </span><span style="background:#100d0c;color:#8ac5ff;">while </span><span style="background:#100d0c;color:#ffffff;">(current </span><span style="background:#100d0c;color:#c0c0c0;">!= </span><span style="background:#100d0c;color:#8ac5ff;">null</span><span style="background:#100d0c;color:#ffffff;">) </span><span style="background:#100d0c;color:#8ac5ff;">if </span><span style="background:#100d0c;color:#ffffff;">(current</span><span style="background:#100d0c;color:#c0c0c0;">.</span><span style="background:#100d0c;color:#ffffff;">Data</span><span style="background:#100d0c;color:#c0c0c0;">.</span><span style="background:#100d0c;color:#ffffff;">Name </span><span style="background:#100d0c;color:#c0c0c0;">== </span><span style="background:#100d0c;color:#ffffff;">s) { t </span><span style="background:#100d0c;color:#c0c0c0;">= </span><span style="background:#100d0c;color:#ffffff;">current</span><span style="background:#100d0c;color:#c0c0c0;">.</span><span style="background:#100d0c;color:#ffffff;">Data; </span><span style="background:#100d0c;color:#8ac5ff;">break</span><span style="background:#100d0c;color:#ffffff;">; } </span><span style="background:#100d0c;color:#8ac5ff;">else </span><span style="background:#100d0c;color:#ffffff;">current </span><span style="background:#100d0c;color:#c0c0c0;">= </span><span style="background:#100d0c;color:#ffffff;">current</span><span style="background:#100d0c;color:#c0c0c0;">.</span><span style="background:#100d0c;color:#ffffff;">Next; </span><span style="background:#100d0c;color:#8ac5ff;">return </span><span style="background:#100d0c;color:#ffffff;">t; } }</span><a href="http://11011.net/software/vspaste"></a></p>
</div>
</div>
<p>The constraint enables the generic class to use the <span class="code">Employee.Name</span> property</p>
<p>because all items of type T are guaranteed to be either an <span class="code">Employee</span> object or an object that inherits from <span class="code">Employee</span>.</p>
<p>Multiple constraints can be applied to the same type parameter, and the constraints themselves can be generic types, as follows:</p>
<div>
<div id="ctl00_LibFrame_MainContent_ctl12_CSharp" class="libCScode">
<div class="CodeSnippetTitleBar">
<div class="CodeDisplayLanguage"> </div>
</div>
</div>
</div>
</div>
<p><span style="background:#16140f;color:#22bdff;">class </span><span style="background:#16140f;color:#e7d97a;">EmployeeList</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#eddfc0;">T</span><span style="background:#16140f;color:#c0c0c0;">&#62; </span><span style="background:#16140f;color:#22bdff;">where </span><span style="background:#16140f;color:#eddfc0;">T </span><span style="background:#16140f;color:#e7696c;">: </span><span style="background:#16140f;color:#eddfc0;">Employee</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#eddfc0;">IEmployee</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#eddfc0;">System</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#dde074;">IComparable</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#eddfc0;">T</span><span style="background:#16140f;color:#c0c0c0;">&#62;</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#22bdff;">new</span><span style="background:#16140f;color:#e7696c;">() { </span><span style="background:#16140f;color:#7cfc00;">// &#8230; </span><span style="background:#16140f;color:#e7696c;">} </span><a href="http://11011.net/software/vspaste"></a></div>
</div>
</div>
</div>
<p>By constraining the type parameter, you increase the number of allowable operations and</p>
<p>method calls to those supported by the constraining type and all types in its inheritance hierarchy. Therefore, when you design generic classes or methods, if you will be performing any operation on the generic members beyond simple assignment or calling any methods not supported by <strong>System.Object</strong>, you will have to apply constraints to the type parameter.</p>
<p>When applying the <span class="code">where T : class</span> constraint, avoid the <span class="keyword">==</span> and <span class="keyword">!=</span> operators on the type parameter because these operators will test for reference identity only, not for value equality. This is the case even if these operators are overloaded in a type that is used as an argument. The following code illustrates this point; the output is false even though the <a id="ctl00_LibFrame_MainContent_ctl13" href="http://msdn.microsoft.com/en-us/library/system.string.aspx">String</a> class overloads the <span class="keyword">==</span> operator.</p>
<div>
<div id="ctl00_LibFrame_MainContent_ctl14_CSharp" class="libCScode">
<div class="CodeSnippetTitleBar">
<div class="CodeDisplayLanguage">
<div class="CopyCodeButton"><a class="copyCode" href="http://msdn.microsoft.com/"></a></div>
</div>
</div>
</div>
</div>
<p><span style="background:#16140f;color:#22bdff;">public static void </span><span style="background:#16140f;color:#eddfc0;">OpTest</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#eddfc0;">T</span><span style="background:#16140f;color:#c0c0c0;">&#62;</span><span style="background:#16140f;color:#e7696c;">(</span><span style="background:#16140f;color:#eddfc0;">T s</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#eddfc0;">T t</span><span style="background:#16140f;color:#e7696c;">) </span><span style="background:#16140f;color:#22bdff;">where </span><span style="background:#16140f;color:#eddfc0;">T </span><span style="background:#16140f;color:#e7696c;">: </span><span style="background:#16140f;color:#22bdff;">class </span><span style="background:#16140f;color:#e7696c;">{ </span></p>
<p><span style="background:#16140f;color:#eddfc0;">       System</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#e7d97a;">Console</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">WriteLine</span><span style="background:#16140f;color:#e7696c;">(</span><span style="background:#16140f;color:#eddfc0;">s </span><span style="background:#16140f;color:#c0c0c0;">== </span><span style="background:#16140f;color:#eddfc0;">t</span><span style="background:#16140f;color:#e7696c;">); </span></p>
<p><span style="background:#16140f;color:#e7696c;">}</span></p>
<p><span style="background:#16140f;color:#e7696c;"> </span><span style="background:#16140f;color:#22bdff;">static void </span><span style="background:#16140f;color:#eddfc0;">Main</span><span style="background:#16140f;color:#e7696c;">() {</span></p>
<p><span style="background:#16140f;color:#e7696c;">           </span><span style="background:#16140f;color:#22bdff;">string </span><span style="background:#16140f;color:#eddfc0;">s1 </span><span style="background:#16140f;color:#c0c0c0;">= </span><span style="background:#16140f;color:#a6f0e6;">&#8220;foo&#8221;</span><span style="background:#16140f;color:#e7696c;">;</span></p>
<p><span style="background:#16140f;color:#e7696c;">            var</span><span style="background:#16140f;color:#e7d97a;"> </span><span style="background:#16140f;color:#eddfc0;">sb </span><span style="background:#16140f;color:#c0c0c0;">= </span><span style="background:#16140f;color:#22bdff;">new </span><span style="background:#16140f;color:#eddfc0;">System</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">Text</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#e7d97a;">StringBuilder</span><span style="background:#16140f;color:#e7696c;">(</span><span style="background:#16140f;color:#a6f0e6;">&#8220;foo&#8221;</span><span style="background:#16140f;color:#e7696c;">); </span></p>
<p><span style="background:#16140f;color:#22bdff;">            string </span><span style="background:#16140f;color:#eddfc0;">s2 </span><span style="background:#16140f;color:#c0c0c0;">= </span><span style="background:#16140f;color:#eddfc0;">sb</span><span style="background:#16140f;color:#c0c0c0;">.</span><span style="background:#16140f;color:#eddfc0;">ToString</span><span style="background:#16140f;color:#e7696c;">(); </span></p>
<p><span style="background:#16140f;color:#eddfc0;">            OpTest</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#22bdff;">string</span><span style="background:#16140f;color:#c0c0c0;">&#62;</span><span style="background:#16140f;color:#e7696c;">(</span><span style="background:#16140f;color:#eddfc0;">s1</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#eddfc0;">s2</span><span style="background:#16140f;color:#e7696c;">); </span></p>
<p><span style="background:#16140f;color:#e7696c;">}</span><a href="http://11011.net/software/vspaste"></a></p>
<p>The reason for this behavior is that, at compile time, the compiler only knows that T is a reference type, and therefore must use the default operators that are valid for all reference types. If you must test for value equality, the recommended way is to also apply the <span class="code">where <strong><em>T : IComparable&#60;T&#62;</em></strong></span> constraint and implement that interface in any class that will be used to construct the generic class.</p>
<div id="ctl00_LibFrame_MainContent_cpe468232" class="MTPS_CollapsibleRegion">
<h2>Unbounded Type Parameters</h2>
<div id="ctl00_LibFrame_MainContent_cpe468232_c" class="MTPS_CollapsibleSection" style="display:block;overflow:visible;width:auto;height:auto;">
<div class="MTPS_CollapsibleSection" style="border-right:medium none;border-top:medium none;display:block;border-left:medium none;border-bottom:medium none;"><a name="sectionToggle1"></a></div>
<p>Type parameters that have no constraints, such as T in public class <span class="code">SampleClass&#60;T&#62;{}</span>, are called unbounded type parameters. Unbounded type parameters have the following rules:</p>
<ul>
<li>The <span class="keyword">!=</span> and <span class="keyword">==</span> operators cannot be used because there is no guarantee that the concrete type argument will support these operators.</li>
<li>They can be converted to and from <strong>System.Object</strong> or explicitly converted to any interface type.</li>
<li>You can compare to <a id="ctl00_LibFrame_MainContent_ctl18" href="http://msdn.microsoft.com/en-us/library/edakx9da.aspx">null</a>. If an unbounded parameter is compared to <span class="keyword">null</span>, the comparison will always return false if the type argument is a value type.</li>
</ul>
</div>
</div>
<div id="ctl00_LibFrame_MainContent_cpe468233" class="MTPS_CollapsibleRegion">
<h2>Naked Type Constraints</h2>
<div id="ctl00_LibFrame_MainContent_cpe468233_c" class="MTPS_CollapsibleSection" style="display:block;overflow:visible;width:auto;height:auto;">
<div class="MTPS_CollapsibleSection" style="border-right:medium none;border-top:medium none;display:block;border-left:medium none;border-bottom:medium none;"><a name="sectionToggle2"></a></div>
<p>When a generic type parameter is used as a constraint, it is called a naked type constraint. Naked type constraints are useful when a member function with its own type parameter has to constrain that parameter to the type parameter of the containing type, as shown in the following example:</p>
<div>
<div id="ctl00_LibFrame_MainContent_ctl23_CSharp" class="libCScode">
<div class="CodeSnippetTitleBar">
<div class="CodeDisplayLanguage">
<div class="CopyCodeButton"><a class="copyCode" href="http://msdn.microsoft.com/"></a></div>
</div>
</div>
</div>
</div>
<p><span style="background:#16140f;color:#22bdff;">class </span><span style="background:#16140f;color:#e7d97a;">List</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#eddfc0;">T</span><span style="background:#16140f;color:#c0c0c0;">&#62; </span><span style="background:#16140f;color:#e7696c;">{ </span><span style="background:#16140f;color:#22bdff;">void </span><span style="background:#16140f;color:#eddfc0;">Add</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#eddfc0;">U</span><span style="background:#16140f;color:#c0c0c0;">&#62;</span><span style="background:#16140f;color:#e7696c;">(</span><span style="background:#16140f;color:#e7d97a;">List</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#eddfc0;">U</span><span style="background:#16140f;color:#c0c0c0;">&#62; </span><span style="background:#16140f;color:#eddfc0;">items</span><span style="background:#16140f;color:#e7696c;">) </span><span style="background:#16140f;color:#22bdff;">where </span><span style="background:#16140f;color:#eddfc0;">U </span><span style="background:#16140f;color:#e7696c;">: </span><span style="background:#16140f;color:#eddfc0;">T </span><span style="background:#16140f;color:#e7696c;">{</span><span style="background:#16140f;color:#7cfc00;">/*&#8230;*/</span><span style="background:#16140f;color:#e7696c;">} }</span><a href="http://11011.net/software/vspaste"></a></div>
</div>
<p>In the previous example, <span class="code">T</span> is a naked type constraint in the context of the <strong>Add</strong> method, and an unbounded type parameter in the context of the <strong>List</strong> class.</p>
<p>Naked type constraints can also be used in generic class definitions. Note that the naked type constraint must also have been declared within the angle brackets together with any other type parameters:</p>
<div>
<div id="ctl00_LibFrame_MainContent_ctl24_CSharp" class="libCScode">
<div class="CodeSnippetTitleBar">
<div class="CodeDisplayLanguage"> </div>
</div>
</div>
</div>
<p><span style="background:#16140f;color:#7cfc00;">//naked type constraint </span></p>
<p><span style="background:#16140f;color:#22bdff;">public class </span><span style="background:#16140f;color:#e7d97a;">SampleClass</span><span style="background:#16140f;color:#c0c0c0;">&#60;</span><span style="background:#16140f;color:#eddfc0;">T</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#eddfc0;">U</span><span style="background:#16140f;color:#e7696c;">, </span><span style="background:#16140f;color:#eddfc0;">V</span><span style="background:#16140f;color:#c0c0c0;">&#62; </span><span style="background:#16140f;color:#22bdff;">where </span><span style="background:#16140f;color:#eddfc0;">T </span><span style="background:#16140f;color:#e7696c;">: </span><span style="background:#16140f;color:#eddfc0;">V </span><span style="background:#16140f;color:#e7696c;">{ } </span><a href="http://11011.net/software/vspaste"></a></p>
<p>The usefulness of naked type constraints with generic classes is very limited because the</p>
<p>compiler can assume nothing about a naked type constraint except that it derives from <strong>System.Object</strong>. Use naked type constraints on generic classes in scenarios in which you want to enforce an inheritance relationship between two type parameters.</p>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:34b43783-74be-487f-823e-ab9d0ff9b750" class="wlWriterSmartContent" style="display:inline;float:none;margin:0;padding:0;">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/C%23">C#</a>,<a rel="tag" href="http://technorati.com/tags/Generics">Generics</a>,<a rel="tag" href="http://technorati.com/tags/Linq">Linq</a>,<a rel="tag" href="http://technorati.com/tags/Core+Knowledge">Core Knowledge</a>,<a rel="tag" href="http://technorati.com/tags/Assumed+Knowledge">Assumed Knowledge</a>,<a rel="tag" href="http://technorati.com/tags/Basics">Basics</a>,<a rel="tag" href="http://technorati.com/tags/.NET+2.0">.NET 2.0</a>,<a rel="tag" href="http://technorati.com/tags/Linq+to+Objects">Linq to Objects</a></div>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:037c91cd-c331-49b2-8a03-22b501814b3b" class="wlWriterSmartContent" style="display:inline;float:none;margin:0;padding:0;">del.icio.us Tags: <a rel="tag" href="http://del.icio.us/popular/C%23">C#</a>,<a rel="tag" href="http://del.icio.us/popular/Generics">Generics</a>,<a rel="tag" href="http://del.icio.us/popular/Linq">Linq</a>,<a rel="tag" href="http://del.icio.us/popular/Core+Knowledge">Core Knowledge</a>,<a rel="tag" href="http://del.icio.us/popular/Assumed+Knowledge">Assumed Knowledge</a>,<a rel="tag" href="http://del.icio.us/popular/Basics">Basics</a>,<a rel="tag" href="http://del.icio.us/popular/.NET+2.0">.NET 2.0</a>,<a rel="tag" href="http://del.icio.us/popular/Linq+to+Objects">Linq to Objects</a></div>
<p>Burak Emir1, Andrew Kennedy2, Claudio Russo2, and Dachuan Yu3</p>
<p>1 EPFL, Lausanne, Switzerland</p>
<p>2 Microsoft Research, Cambridge, U.K.</p>
<p>3 DoCoMo Communications Laboratories USA, San Jose, California</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[a couple of LINQ tools]]></title>
<link>http://ratnakarg.wordpress.com/2008/05/04/some-useful-linq-tools/</link>
<pubDate>Sun, 04 May 2008 00:00:16 +0000</pubDate>
<dc:creator>Ratnakar Garikipati</dc:creator>
<guid>http://ratnakarg.wordpress.com/2008/05/04/some-useful-linq-tools/</guid>
<description><![CDATA[Visual LINQ Query Builder   Visual LINQ Query Builder is an add-in to Visual Studio 2008 Designer th]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><div><span style="font-size:x-small;font-family:Arial;"></p>
<p class="MsoNormal" style="margin:0;"><strong><span style="text-decoration:underline;"><span style="font-size:10pt;color:#e36c0a;"><a href="http://code.msdn.microsoft.com/vlinq"><span style="color:#000080;"><span>Visual LINQ Query Builder</span></span></a> </span></span></strong><span style="font-size:10pt;color:#1f497d;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;">Visual LINQ Query Builder is an <strong>add-in</strong> to <strong>Visual Studio 2008 Designer</strong> that&#8217;s developed to help you build LINQ to SQL queries through a friendly interface. </span><span style="font-size:10pt;">The tool can be seen in action here -<span style="color:#080808;"> </span><span style="text-decoration:underline;"><span><a href="http://blogs.msdn.com/mitsu/archive/2008/04/02/visual-linq-query-builder-for-linq-to-sql-vlinq.aspx"><span style="color:#000080;">http://blogs.msdn.com/mitsu/archive/2008/04/02/visual-linq-query-builder-for-linq-to-sql-vlinq.aspx</span></a><span style="color:#000080;"> </span></span></span><span style="color:#1f497d;"><span> </span></span></span><span style="font-size:10pt;">and available for download from the MSDN Code Gallery -<span style="color:#000080;"> </span><span style="text-decoration:underline;"><span style="color:#e36c0a;"><a href="http://code.msdn.microsoft.com/vlinq"><span style="color:#000080;">http://code.msdn.microsoft.com/vlinq</span></a><br />
</span></span></span><span style="font-size:10pt;color:#1f497d;"> </span></p>
<p class="MsoNormal" style="margin:0;"><strong><span style="font-size:10pt;"><a href="http://www.linqpad.net/"><span style="color:#000080;">LINQPad</span></a></span></strong><span style="font-size:10pt;color:#000080;"> </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;">LINQPad is proving to be quite successful and seen as a tool that’s not only useful to learn LINQ but also for dev purposes. </span><span style="font-size:10pt;"> </span><span style="font-size:10pt;">This tool: <span> </span><span> </span></span></p>
<p class="MsoListParagraph" style="text-indent:-18pt;margin:0 0 0 54pt;"><span style="font-size:10pt;"><span>-<span>       </span></span></span><span style="font-size:10pt;">supports C# 3.0 and Framework 3.5 (LINQ to SQL, LINQ to Objects, LINQ to XML)</span></p>
<p class="MsoListParagraph" style="text-indent:-18pt;margin:0 0 0 54pt;"><span style="font-size:10pt;"><span>-<span>       </span></span></span><span style="font-size:10pt;">comes with 200 preloaded examples</span></p>
<p class="MsoListParagraph" style="text-indent:-18pt;margin:0 0 0 54pt;"><span style="font-size:10pt;"><span>-<span>       </span></span></span><span style="font-size:10pt;">offers code snippet IDE </span></p>
<p class="MsoListParagraph" style="text-indent:-18pt;margin:0 0 0 54pt;"><span style="font-size:10pt;"><span>-<span>       </span></span></span><span style="font-size:10pt;">free and only 2mb in size</span></p>
<p class="MsoListParagraph" style="text-indent:-18pt;margin:0 0 0 54pt;"><span style="font-size:10pt;">more here -<span style="color:#1f497d;"> <span><span style="color:#c0c0c0;"> </span></span></span><span style="text-decoration:underline;"><span><span style="color:#000080;"><a href="http://www.linqpad.net/">http://www.linqpad.net/</a><br />
</span></span></span></span></p>
<p></span></div>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[101 Linq Samples]]></title>
<link>http://pythonyan.wordpress.com/2008/04/23/101-linq-samples/</link>
<pubDate>Wed, 23 Apr 2008 13:15:18 +0000</pubDate>
<dc:creator>pythonyan</dc:creator>
<guid>http://pythonyan.wordpress.com/2008/04/23/101-linq-samples/</guid>
<description><![CDATA[Su Msdn è presente questa risorsa molto utile per capire Linq, orientato all&#8217; utilizzo con gli]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Su Msdn è presente <a title="101 Linq Samples" href="http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx">questa </a>risorsa molto utile per capire Linq, orientato all&#8217; utilizzo con gli Object.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[LINQ to Objects (II)]]></title>
<link>http://fjaumot.wordpress.com/2007/11/28/linq-to-objects-ii/</link>
<pubDate>Wed, 28 Nov 2007 12:14:58 +0000</pubDate>
<dc:creator>fjaumot</dc:creator>
<guid>http://fjaumot.wordpress.com/2007/11/28/linq-to-objects-ii/</guid>
<description><![CDATA[LINQ to Objects permite a los desarrolladores hacer consultas sobre una colección de objetos con una]]></description>
<content:encoded><![CDATA[LINQ to Objects permite a los desarrolladores hacer consultas sobre una colección de objetos con una]]></content:encoded>
</item>
<item>
<title><![CDATA[Que es LINQ? (I)]]></title>
<link>http://fjaumot.wordpress.com/2007/11/23/que-es-linq-i/</link>
<pubDate>Fri, 23 Nov 2007 12:38:53 +0000</pubDate>
<dc:creator>fjaumot</dc:creator>
<guid>http://fjaumot.wordpress.com/2007/11/23/que-es-linq-i/</guid>
<description><![CDATA[LINQ es, además de otra gozada de Microsoft, una extensión funcional para C# y VB que nos permite ha]]></description>
<content:encoded><![CDATA[LINQ es, además de otra gozada de Microsoft, una extensión funcional para C# y VB que nos permite ha]]></content:encoded>
</item>

</channel>
</rss>
