# C1.LiveLinq.LiveViews.View-1.AttachView

## Content

<div class="doc-site-dotnet-api-container">



<h1 id="C1_LiveLinq_LiveViews_View_1_AttachView_" data-uid="C1.LiveLinq.LiveViews.View`1.AttachView*">AttachView Method
</h1>
<div class="markdown level0 summary"></div>
<div class="markdown level0 conceptual"></div>



<a id="C1_LiveLinq_LiveViews_View_1_AttachView_" data-uid="C1.LiveLinq.LiveViews.View`1.AttachView*"></a>
<h4 id="C1_LiveLinq_LiveViews_View_1_AttachView__1_System_Func_C1_LiveLinq_LiveViews_View__0__C1_LiveLinq_LiveViews_View___0___" data-uid="C1.LiveLinq.LiveViews.View`1.AttachView``1(System.Func{C1.LiveLinq.LiveViews.View{`0},C1.LiveLinq.LiveViews.View{``0}})">AttachView&lt;TResult&gt;(Func&lt;View&lt;T&gt;, View&lt;TResult&gt;&gt;)</h4>
<div class="markdown level1 summary"><p>Includes a subquery into the incremental maintenance mechanism of a view.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="declaration">Declaration</h5>
<div class="codewrapper">
  <pre><code class="lang-csharp hljs">public View&lt;TResult&gt; AttachView&lt;TResult&gt;(Func&lt;View&lt;T&gt;, View&lt;TResult&gt;&gt; selector)</code></pre>
</div>
<div class="codewrapper">
  <pre><code class="lang-vbnet hljs">Public Function AttachView(Of TResult)(selector As Func(Of View(Of T), View(Of TResult))) As View(Of TResult)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.func-2">Func</a>&lt;<a class="xref" href="C1.LiveLinq.LiveViews.View-1.html">View</a>&lt;T&gt;, <a class="xref" href="C1.LiveLinq.LiveViews.View-1.html">View</a>&lt;TResult&gt;&gt;</td>
      <td><span class="parametername">selector</span></td>
      <td><p>A function to obtain the attached subview from the view to which it is attached.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 class="returns">Returns</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="C1.LiveLinq.LiveViews.View-1.html">View</a>&lt;TResult&gt;</td>
      <td><p>The attached subview.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 class="typeParameters">Type Parameters</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><span class="parametername">TResult</span></td>
      <td><p>The type of the elements in the subquery view.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 id="C1_LiveLinq_LiveViews_View_1_AttachView__1_System_Func_C1_LiveLinq_LiveViews_View__0__C1_LiveLinq_LiveViews_View___0____remarks">Remarks</h5>
<div class="markdown level1 remarks"><p>Subquery is a query inside a function that is a part of an outer query.
If the outer query is a live view, and its base data changes so it needs to recompute the function,
by default it just evaluates the function, which means creating a new view object for the subquery on every such recomputation.
To improve performance by avoiding repeated creation and population of subviews, use the <b>AttachView</b> method to make the
outer view aware of its subview. Then the outer view will cache and reuse subview objects it creates.</p>
<p>If there are more than one subviews attached to a single view, every 
subview must be supplied with a unique <i>subview id</i> by using the <b>AttachView</b> overload with <i>subviewId</i> parameter.</p>
<example>
<pre><code class="lang-csharp">var productView =
    from p in products
    join s in sales on p.ProductID equals s.ProductID into productSales
    select new
    {
        p.Name,
        SalesByCountry = productSales.AttachView(v =&gt;
            from s in v
            group s by s.Country into countrySales
            select new
            {
                Country = countrySales.Key,
                SaleCount = countrySales.LiveSum(s =&gt; s.Quantity).Value
            })
    };</code></pre>
</example>
</div>
<h5 id="C1_LiveLinq_LiveViews_View_1_AttachView__1_System_Func_C1_LiveLinq_LiveViews_View__0__C1_LiveLinq_LiveViews_View___0____seealso">See Also</h5>
<div class="seealso">
    <div><a class="xref" href="C1.LiveLinq.LiveViews.View-1.AttachAggregationView.html#C1_LiveLinq_LiveViews_View_1_AttachAggregationView__1_System_Func_C1_LiveLinq_LiveViews_View__0__C1_LiveLinq_LiveViews_AggregationView__0___0___">AttachAggregationView</a>&lt;TResult&gt;(<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.func-2">Func</a>&lt;<a class="xref" href="C1.LiveLinq.LiveViews.View-1.html">View</a>&lt;T&gt;, <a class="xref" href="C1.LiveLinq.LiveViews.AggregationView-2.html">AggregationView</a>&lt;T, TResult&gt;&gt;)</div>
</div>


<a id="C1_LiveLinq_LiveViews_View_1_AttachView_" data-uid="C1.LiveLinq.LiveViews.View`1.AttachView*"></a>
<h4 id="C1_LiveLinq_LiveViews_View_1_AttachView__1_System_Object_System_Func_C1_LiveLinq_LiveViews_View__0__C1_LiveLinq_LiveViews_View___0___" data-uid="C1.LiveLinq.LiveViews.View`1.AttachView``1(System.Object,System.Func{C1.LiveLinq.LiveViews.View{`0},C1.LiveLinq.LiveViews.View{``0}})">AttachView&lt;TResult&gt;(object, Func&lt;View&lt;T&gt;, View&lt;TResult&gt;&gt;)</h4>
<div class="markdown level1 summary"><p>Includes a subquery into the incremental maintenance mechanism of a view and uniquely identifies it among other subqueries of the same view.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="declaration">Declaration</h5>
<div class="codewrapper">
  <pre><code class="lang-csharp hljs">public View&lt;TResult&gt; AttachView&lt;TResult&gt;(object subqueryId, Func&lt;View&lt;T&gt;, View&lt;TResult&gt;&gt; selector)</code></pre>
</div>
<div class="codewrapper">
  <pre><code class="lang-vbnet hljs">Public Function AttachView(Of TResult)(subqueryId As Object, selector As Func(Of View(Of T), View(Of TResult))) As View(Of TResult)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object">object</a></td>
      <td><span class="parametername">subqueryId</span></td>
      <td><p>A string uniquely specifying this subquery in the view to which it is attached. Can be any string.
The only requirement is that different subqueries attached to the same view, if such exist, must have different subquery ids.</p>
</td>
    </tr>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.func-2">Func</a>&lt;<a class="xref" href="C1.LiveLinq.LiveViews.View-1.html">View</a>&lt;T&gt;, <a class="xref" href="C1.LiveLinq.LiveViews.View-1.html">View</a>&lt;TResult&gt;&gt;</td>
      <td><span class="parametername">selector</span></td>
      <td><p>A function to obtain the attached subview from the view to which it is attached.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 class="returns">Returns</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="C1.LiveLinq.LiveViews.View-1.html">View</a>&lt;TResult&gt;</td>
      <td><p>The attached subview.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 class="typeParameters">Type Parameters</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><span class="parametername">TResult</span></td>
      <td><p>The type of the elements in the subquery view.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 id="C1_LiveLinq_LiveViews_View_1_AttachView__1_System_Object_System_Func_C1_LiveLinq_LiveViews_View__0__C1_LiveLinq_LiveViews_View___0____remarks">Remarks</h5>
<div class="markdown level1 remarks"><p>This overload must be used if you have several subviews attached to the same view.
The <i>subqueryId</i> can be any string as long as it is not repeated for two different subviews attached to the same view.</p>
<p>See the other overload for the explanation of the <b>AttachView</b> method.</p>
</div>
<h5 id="C1_LiveLinq_LiveViews_View_1_AttachView__1_System_Object_System_Func_C1_LiveLinq_LiveViews_View__0__C1_LiveLinq_LiveViews_View___0____seealso">See Also</h5>
<div class="seealso">
    <div><a class="xref" href="C1.LiveLinq.LiveViews.View-1.AttachAggregationView.html#C1_LiveLinq_LiveViews_View_1_AttachAggregationView__1_System_Object_System_Func_C1_LiveLinq_LiveViews_View__0__C1_LiveLinq_LiveViews_AggregationView__0___0___">AttachAggregationView</a>&lt;TResult&gt;(<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object">object</a>, <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.func-2">Func</a>&lt;<a class="xref" href="C1.LiveLinq.LiveViews.View-1.html">View</a>&lt;T&gt;, <a class="xref" href="C1.LiveLinq.LiveViews.AggregationView-2.html">AggregationView</a>&lt;T, TResult&gt;&gt;)</div>
</div>
</div>
