# C1.LiveLinq.Indexing.IndexingAlgorithm.CreateIndex

## Content

<div class="doc-site-dotnet-api-container">



<h1 id="C1_LiveLinq_Indexing_IndexingAlgorithm_CreateIndex_" data-uid="C1.LiveLinq.Indexing.IndexingAlgorithm.CreateIndex*">CreateIndex Method
</h1>
<div class="markdown level0 summary"></div>
<div class="markdown level0 conceptual"></div>



<a id="C1_LiveLinq_Indexing_IndexingAlgorithm_CreateIndex_" data-uid="C1.LiveLinq.Indexing.IndexingAlgorithm.CreateIndex*"></a>
<h4 id="C1_LiveLinq_Indexing_IndexingAlgorithm_CreateIndex__2_C1_LiveLinq_IObservableSource___0__System_Linq_Expressions_Expression_System_Func___0___1___System_Boolean_System_Globalization_CultureInfo_" data-uid="C1.LiveLinq.Indexing.IndexingAlgorithm.CreateIndex``2(C1.LiveLinq.IObservableSource{``0},System.Linq.Expressions.Expression{System.Func{``0,``1}},System.Boolean,System.Globalization.CultureInfo)">CreateIndex&lt;T, TKey&gt;(IObservableSource&lt;T&gt;, Expression&lt;Func&lt;T, TKey&gt;&gt;, bool, CultureInfo)</h4>
<div class="markdown level1 summary"><p>Creates a new index.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="declaration">Declaration</h5>
<div class="codewrapper">
  <pre><code class="lang-csharp hljs">public Index&lt;T, TKey&gt; CreateIndex&lt;T, TKey&gt;(IObservableSource&lt;T&gt; source, Expression&lt;Func&lt;T, TKey&gt;&gt; keySelector, bool keyIsUnique, CultureInfo locale)</code></pre>
</div>
<div class="codewrapper">
  <pre><code class="lang-vbnet hljs">Public Function CreateIndex(Of T, TKey)(source As IObservableSource(Of T), keySelector As Expression(Of Func(Of T, TKey)), keyIsUnique As Boolean, locale As CultureInfo) As Index(Of T, TKey)</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="C1.LiveLinq.IObservableSource-1.html">IObservableSource</a>&lt;T&gt;</td>
      <td><span class="parametername">source</span></td>
      <td><p>The collection to be indexed.</p>
</td>
    </tr>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.linq.expressions.expression-1">Expression</a>&lt;<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.func-2">Func</a>&lt;T, TKey&gt;&gt;</td>
      <td><span class="parametername">keySelector</span></td>
      <td><p>Key selector expression of the index, see <a class="xref" href="C1.LiveLinq.Indexing.IndexDefinition-1.KeySelector.html#C1_LiveLinq_Indexing_IndexDefinition_1_KeySelector">KeySelector</a>.</p>
</td>
    </tr>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.boolean">bool</a></td>
      <td><span class="parametername">keyIsUnique</span></td>
      <td><p>Specifies whether the key used in this index is a unique key for the indexed collection.</p>
</td>
    </tr>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.globalization.cultureinfo">CultureInfo</a></td>
      <td><span class="parametername">locale</span></td>
      <td><p>Locale information used to compare strings in the index (default: <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.globalization.cultureinfo.currentculture">CultureInfo.CurrentCulture</a>).</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.Indexing.Index-2.html">Index</a>&lt;T, TKey&gt;</td>
      <td><p>The new index.</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">T</span></td>
      <td><p>The type of the elements of the collection to index.</p>
</td>
    </tr>
    <tr>
      <td><span class="parametername">TKey</span></td>
      <td><p>The type of the index key.</p>
</td>
    </tr>
  </tbody>
</table>
<h5 id="C1_LiveLinq_Indexing_IndexingAlgorithm_CreateIndex__2_C1_LiveLinq_IObservableSource___0__System_Linq_Expressions_Expression_System_Func___0___1___System_Boolean_System_Globalization_CultureInfo__remarks">Remarks</h5>
<div class="markdown level1 remarks"><p>Normally, you don't need to use this method. Indexes are usually created in code by calling 
<a class="xref" href="C1.LiveLinq.Indexing.IndexCollection-1.Add.html#C1_LiveLinq_Indexing_IndexCollection_1_Add__1_System_Linq_Expressions_Expression_System_Func__0___0___">IndexCollection.Add</a>, or their creation
can be enforced in LINQ queries by using the <a class="xref" href="C1.LiveLinq.Hints.Indexed.html#C1_LiveLinq_Hints_Indexed__1___0_C1_LiveLinq_IndexingHintAction_">Indexed</a> hint.
This method should be used only in special situations where all you want is to index a collection without an overhead of maintaining a collection of indexes,
or you need an index that exists separately from the collection of indexes maintained by the source.
Unlike the standard ways of creating indexes, this method only creates an index object and attaches it to the source
(so it will be automatically synchronized with the source when the source is modified), but the created index is 
not added to <a class="xref" href="C1.LiveLinq.Indexing.IndexCollection-1.html">IndexCollection&lt;T&gt;</a>.</p>
<p>A unique index occupies less memory and performs better than a non-unique index (although the difference isn't dramatic).
Therefore, for unique keys, it's recommended to specify the corresponding index as unique.
But do that only if you are sure that the key is indeed unique, as it imposes a uniqueness constraint on the indexed collection.
An attempt to modify the indexed collection violating the uniqueness throws an <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.invalidoperationexception">InvalidOperationException</a>.</p>
</div>
</div>
