---
title: "How to linq join with groups?"  
description: "How to linq join with groups?"  
author: "Anurag Sharma"  
published: 2014-11-04  
updated: 2014-11-04  
canonical: https://www.mindstick.com/forum/2507/how-to-linq-join-with-groups  
category: "linq"  
tags: ["c#", "join"]  
reading_time: 2 minutes  

---

# How to linq join with groups?

Hej, i've got a [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) with [linq expression](https://www.mindstick.com/forum/12960/how-do-i-get-a-single-row-from-a-linq-expression-in-c-sharp). I Would like to perform [join](https://www.mindstick.com/articles/1487/join-sleep-and-interrupt-methods-in-c-sharp-threading) with [groups](https://www.mindstick.com/interview/34156/what-are-groups-in-signalr-and-how-can-you-use-them-to-manage-chat-rooms) of some set. This is basicly what I need:

```
var f = new IEnumerable<SomeClass> { ... };var rows = new IEnumerable<SomeOtherClass> { ... };var makedGroups = rows.GroupBy(row => row.SomeId);var groupsJoined = (from row in makedGroups    join allocQuant in f on row.Key.Value equals allocQuant.SomeId into gj    select gr => new { Count = gr.Count(), Group = gj });
```

\

[error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) is: Error 202 The type of one of the [expressions](https://www.mindstick.com/forum/33876/what-is-the-syntax-for-lambda-expressions-in-vb-dot-net) in the join [clause](https://www.mindstick.com/forum/33937/difference-between-throw-exception-and-throw-clause) is incorrect. Type inference [failed](https://www.mindstick.com/forum/160484/error-failed-to-launch-debug-adapter-additional-information-may-be-available-in-the-output-window) in the call to 'Join'.

## How to write this expression properly?

## Replies

### Reply by David Miller

No longer have access to the range variables used in the initial from. That is, we can no longer talk about p or bp, you can only talk about pg.

Now, pg is a group and so contains more than one product. All the products in a given pg group have the same SomeId (since that's what you grouped by), but I don't know if that means they all have the same BaseProductId.\

To get a base product name, you have to pick a particular product in the pg group (As you are doing with SomeId and CountryCode), and then join to BaseProducts.\

```
var result = from p in Products                          group p by p.SomeId into pg                          // join *after* group join bp in BaseProducts on pg.FirstOrDefault().BaseProductId equals bp.Id          select new ProductPriceMinMax {        SomeId = pg.FirstOrDefault().SomeId,        CountryCode = pg.FirstOrDefault().CountryCode,        MinPrice = pg.Min(m => m.Price),        MaxPrice = pg.Max(m => m.Price),       BaseProductName = bp.Name  // now there is a 'bp' in scope };
```


---

Original Source: https://www.mindstick.com/forum/2507/how-to-linq-join-with-groups

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
