how to convert  a php Script to c#
<pre><code>&lt;?php

$list = array(
    array(
        'id' =&gt; '1',
        'pid' =&gt; '2',
        'value' =&gt; 'Value Foo 1',
    ),
    array(
        'id' =&gt; '2',
        'pid' =&gt; '6',
        'value' =&gt; 'Value Foo 2',
    ),
    array(
        'id' =&gt; '3',
        'pid' =&gt; '5',
        'value' =&gt; 'Value Foo 3',
    ),
    array(
        'id' =&gt; '4',
        'pid' =&gt; '2',
        'value' =&gt; 'Value Foo 4',
    ),
    array(
        'id' =&gt; '5',
        'pid' =&gt; '0',
        'value' =&gt; 'Value Foo 5',
    ),
    array(
        'id' =&gt; '6',
        'pid' =&gt; '0',
        'value' =&gt; 'Value Foo 6',
    ),
    array(
        'id' =&gt; '7',
        'pid' =&gt; '6',
        'value' =&gt; 'Value Foo 7',
    ),
    array(
       'id' =&gt; '8',
       'pid' =&gt; '2',
       'value' =&gt; 'Value Foo 8',
    ),
    array(
        'id' =&gt; '9',
        'pid' =&gt; '5',
        'value' =&gt; 'Value Foo 9',
    ),
);

//
// Creating a lookup array
//
$lookup = array();
foreach( $list as $item )
{
    $item['children'] = array();
    $lookup[$item['id']] = $item;
}

//
// Now build tree.
//
$tree = array();
foreach( $lookup as $id =&gt; $foo )
{
    $item = &amp;$lookup[$id];
    if( $item['pid'] == 0 )
    {
        $tree[$id] = &amp;$item;
    }
    else if(isset($lookup[$item['pid']]))
    {
        $lookup[$item['pid']]['children'][$id] = &amp;$item;
    }
    else
    {
        $tree['_orphans_'][$id] = &amp;$item;
    }
}

print_r( $tree );
?&gt;
</code></pre>

<p>This is what i Have in c#</p>

<pre><code>    public class Thing { 
             public String _id; 
             public int Bar; 
             public string _pid; 
    }
          var items = new[] {
                 new Thing { _id = "item1", _pid = "root", Baz = "a" },
                 new Thing {_id = "item2", _pid = "item1", Baz= "b" },
                 new Thing { _id = "item3", _pid = "item1", Baz = "c" },
                 new Thing { _id = "item4", _pid = "item1", Baz = "d" },
                 new Thing { _id = "item5", _pid = "item1", Baz = "e" },
                 new Thing { _id = "item6", _pid = "item1", Baz = "f" }
              };
//Then I create a lookup 
ILookup&lt;string, Thing&gt; lookup = items.ToLookup(i =&gt; i._id); 

//here is when I want to create a condition for each item in lookup to search if the _pid is in //the lookup as an item _id

foreach (IGrouping&lt;string, Thing&gt; item in lookup){ 

//here I dont know how to add a children object to the parent item and add a reference to the //current item 
//this is how Iam doing in php 
if(isset($lookup[$item['_pid']])) { 
$lookup[$item['_pid']]['children'][_id] = &amp;$item; //&lt;-item is passed by reference 
}
</code></pre>

<p>Thanks..
Regards </p>
