# GroupProcessor

&#x20;The `GroupProcessor` class is a generic class that provides parallel processing functionality for collections of elements. It allows users to easily perform parallel operations on a group of items and return the results as different types of collections, such as `ConcurrentBag`, `ConcurrentDictionary`, or a `List`.

The "Group" Processor is for a selection where more than one item can be matched for the given selector, also known as a **GroupBy**. If you're looking for a single item in and out without grouping, use the [SingleProcessor](/core-modules/perigee-in-parallel/singleprocessor.md).

## ParallelProcess

This method processes the collection in parallel and invokes the provided callback for each group of items with the same key.

#### Example:

```csharp
var groupProcessor = new GroupProcessor<Item, Category>(items, item => item.Category);
groupProcessor.ParallelProcess((group, category) => {
    Console.WriteLine($"Processing category: {category}");
    foreach (var item in group)
    {
        Console.WriteLine($"  - {item.Name}");
    }
});
```

## ParallelProcessToBag

This method processes the collection in parallel and adds the results to a `ConcurrentBag` by invoking the provided callback for each group of items with the same key.

#### Example:

```csharp
var groupProcessor = new GroupProcessor<Item, Category>(items, item => item.Category);
ConcurrentBag<string> resultBag = groupProcessor.ParallelProcessToBag((group, category) => {
    int itemCount = group.Count;
    return $"Category: {category}, Item Count: {itemCount}";
});

```

## ParallelProcessToDictionary

This method processes the collection in parallel and adds the results to a `ConcurrentDictionary` by invoking the provided callback for each group of items with the same key.

#### Example:

```csharp
var groupProcessor = new GroupProcessor<Item, Category>(items, item => item.Category);
ConcurrentDictionary<Category, int> resultDictionary = groupProcessor.ParallelProcessToDictionary((group, category) => {
    int itemCount = group.Count;
    return new KeyValuePair<Category, int>(category, itemCount);
});
```

## ParallelProcessKeys

This method processes the keys of the collection in parallel and invokes the provided callback for each key.

#### Example:

```csharp
var singleProcessor = new SingleProcessor<Item, Category>(items, item => item.Category);
singleProcessor.ParallelProcessKeys(key => {
    Console.WriteLine($"Processing key: {key}");
});
```

## ParallelProcessKeysToBag

This method processes the keys of the collection in parallel and adds the results to a `ConcurrentBag` by invoking the provided callback for each key.

#### Example:

```csharp
var singleProcessor = new SingleProcessor<Item, Category>(items, item => item.Category);
ConcurrentBag<string> resultBag = singleProcessor.ParallelProcessKeysToBag(key => {
    return $"Key: {key}";
});
```

## ParallelProcessKeysToDictionary

This method processes the keys of the collection in parallel and adds the results to a `ConcurrentDictionary` by invoking the provided callback for each key.

#### Example:

```csharp
var singleProcessor = new SingleProcessor<Item, Category>(items, item => item.Category);
ConcurrentDictionary<Category, int> resultDictionary = singleProcessor.ParallelProcessKeysToDictionary(key => {
    return new KeyValuePair<Category, int>(key, 1);
});
```

## ParallelToNewSingleProcessor

This method processes the keys of the collection in parallel and returns a new `SingleProcessor` instance containing the results.

#### Example:

```csharp
var singleProcessor = new SingleProcessor<Item, Category>(items, item => item.Category);
SingleProcessor<string, Category> newSingleProcessor = singleProcessor.ParallelToNewSingleProcessor(key => {
    return new KeyValuePair<Category, string>(key, $"New {key}");
});
```

## ParallelToNewGroupedProcessor

This method processes the keys of the collection in parallel and returns a new `GroupProcessor` instance containing the results, grouped by the provided expression.

#### Example:

```csharp
var singleProcessor = new SingleProcessor<Item, Category>(items, item => item.Category);
GroupProcessor<string, Category> newGroupProcessor = singleProcessor.ParallelToNewGroupedProcessor(key => {
    return new KeyValuePair<Category, string>(key, $"New {key}");
}, group => group.Category);
```

## AllKeys

Returns all keys in the processor as an enumerable.

#### Example:

```csharp
var allKeys = processor.AllKeys();
foreach (var key in allKeys)
{
    Console.WriteLine(key);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.perigee.software/core-modules/perigee-in-parallel/groupprocessor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
