> For the complete documentation index, see [llms.txt](https://dailyjournal.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dailyjournal.gitbook.io/notes/languages/c-sharp/features/linq/working-with-data.md).

# Working with Data

## Sorting

<table><thead><tr><th width="203">Method Name</th><th width="282">Description</th><th>C# Query Expression Syntax</th></tr></thead><tbody><tr><td>OrderBy</td><td>Sorts values in ascending order.</td><td><code>orderby</code></td></tr><tr><td>OrderByDescending</td><td>Sorts values in descending order.</td><td><code>orderby … descending</code></td></tr><tr><td>ThenBy</td><td>Performs a secondary sort in ascending order.</td><td><code>orderby …, …</code></td></tr><tr><td>ThenByDescending</td><td>Performs a secondary sort in descending order.</td><td><code>orderby …, … descending</code></td></tr><tr><td>Reverse</td><td>Reverses the order of the elements in a collection.</td><td>NA</td></tr></tbody></table>

<pre class="language-csharp"><code class="lang-csharp">string[] words = {"the", "quick", "brown", "fox", "jumps"};

IEnumerable&#x3C;string> queryStx =
	from word in words
<strong>	orderby word.Length
</strong>	select word;

<strong>IEnumerable&#x3C;string> methodStx = words.OrderBy(w => w.Length);
</strong>
foreach (string str in queryStx)
	Console.Write(str + " ");

/* Output : the fox quick brown jumps */
</code></pre>

## Filtering

<table><thead><tr><th width="160">Method Name</th><th width="334">Description</th><th>C# Query Expression Syntax</th></tr></thead><tbody><tr><td>OfType</td><td>Selects values, depending on their ability to be cast to a specified type.</td><td>NA</td></tr><tr><td>Where</td><td>Selects values that are based on a predicate function.</td><td><code>where</code></td></tr></tbody></table>

<pre class="language-csharp"><code class="lang-csharp">string[] words = {"the", "quick", "brown", "fox", "jumps"};

IEnumerable&#x3C;string> queryStx =
	from word in words
<strong>	where word.Length == 3
</strong>	select word;

<strong>IEnumerable&#x3C;string> methodStx = words.Where(w => w.Length == 3);
</strong>
foreach (string str in methodStx)
	Console.Write(str + " "); 

/* Output: the fox */
</code></pre>

## Partitioning

<table><thead><tr><th width="168">Method names</th><th>Description</th></tr></thead><tbody><tr><td>Skip</td><td>Skips elements up to a specified position in a sequence.</td></tr><tr><td>SkipWhile</td><td>Skips elements based on a predicate function until an element does not satisfy the condition.</td></tr><tr><td>Take</td><td>Takes elements up to a specified position in a sequence.</td></tr><tr><td>TakeWhile</td><td>Takes elements based on a predicate function until an element does not satisfy the condition.</td></tr><tr><td>Chunk</td><td>Splits the elements of a sequence into chunks of a specified maximum size.</td></tr></tbody></table>

<pre class="language-csharp"><code class="lang-csharp">int[] numbers = {5, 9, 7, 2, 4, 3, 6, 1, 8};
<strong>foreach (var num in numbers.Take(3)) {
</strong>	Console.Write(num + "\t");
} /* Output : 5    9    7 */
<strong>foreach (var num in numbers.Skip(3)) {
</strong>	Console.Write(num + "\t");
} /* Output : 2    4    3    6    1    8 */
</code></pre>

<pre class="language-csharp"><code class="lang-csharp">int chunkNumber = 1;
<strong>foreach (int[] chunk in Enumerable.Range(0, 9).Chunk(3)) {
</strong>	Console.Write($"Chunk {chunkNumber++}:");
	foreach (int item in chunk) {
		Console.Write($"    {item}");
	}

	Console.WriteLine();
}
// Output:
// Chunk 1:    0    1    2
// Chunk 2:    3    4    5
// Chunk 3:    6    7    8

</code></pre>

## Grouping

<table><thead><tr><th width="156.33333333333331">Method Name</th><th width="339">Description</th><th>C# Query Expression Syntax</th></tr></thead><tbody><tr><td>GroupBy</td><td>Groups elements that share a common attribute. Each group is represented by an <a href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.igrouping-2">IGrouping&#x3C;TKey,TElement></a> object.</td><td><code>group … by</code><br>-or-<br><code>group … by … into …</code></td></tr><tr><td>ToLookup</td><td>Inserts elements into a <a href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.lookup-2">Lookup&#x3C;TKey,TElement></a> (a one-to-many dictionary) based on a key selector function.</td><td>NA</td></tr></tbody></table>

<pre class="language-csharp"><code class="lang-csharp">List&#x3C;int> numbers = new List&#x3C;int>(){35, 44, 200, 84, 3987, 4, 199, 329, 446, 208};
IEnumerable&#x3C;IGrouping&#x3C;int, int>> query =
	from number in numbers
<strong>	group number by number % 2;
</strong>	
foreach (var group in query) {
	Console.Write(group.Key == 0 ? "\nEven numbers: " : "\nOdd numbers: ");
	foreach (int i in group)
		Console.Write(i + " ");
}
</code></pre>
