GroupBy a list of key-value pairs then regroup the resultLinq List of Objects GroupBy Multiple Property EqualityLINQ GroupBy a List according to a key inside dictionary inside dictionary inside List C#How to get a list of the grouped values in linq groupby?Group a sorted list without affecting the sorting in linqDynamically create anonymous object from list values c#How way to operate on single items at an index in group in IGrouping?Getting unique/distinct list of key value pairs in C#Group data having unique keys and valuesFilter data using list of dynamic unique key value pairs linq/lamda expressionList object property as Key in LINQ GroupBy clause

Why was Sauron not trying to find the Ring, and instead of preparing for war?

On the strategic interest of giving long lasting stock orders

Is there a reason why I should not use the HaveIBeenPwned API to warn users about exposed passwords?

What is the meaning of "you has the wind of me"?

Creating Darkness

What are the exact meanings of roll, pitch and yaw?

Iterate over non-const variables in C++

Replacing tongue and groove floorboards: but can't find a match

Move a group of files, prompting the user for confirmation for every file

How important is a good quality camera for good photography?

Terence Tao–type books in other fields?

How can I stop myself from micromanaging other PCs' actions?

What is the difference between 1/3, 1/2, and full casters?

How to copy a file transactionally?

Why no ";" after "do" in sh loops?

Strange Cron Job takes up 100% of CPU Ubuntu 18 LTS Server

Print sums of all subsets

Can I go to the UK from the Schengen on train?

(1 of 11: Numberlink) What is Pyramid Cult's Favorite Activity?

Why is chess failing to attract big name sponsors?

How do campaign rallies gain candidates votes?

Does academia have a lazy work culture?

Why is my read in of data taking so long?

Which Roman general was killed by his own soldiers for not letting them to loot a newly conquered city?



GroupBy a list of key-value pairs then regroup the result


Linq List of Objects GroupBy Multiple Property EqualityLINQ GroupBy a List according to a key inside dictionary inside dictionary inside List C#How to get a list of the grouped values in linq groupby?Group a sorted list without affecting the sorting in linqDynamically create anonymous object from list values c#How way to operate on single items at an index in group in IGrouping?Getting unique/distinct list of key value pairs in C#Group data having unique keys and valuesFilter data using list of dynamic unique key value pairs linq/lamda expressionList object property as Key in LINQ GroupBy clause






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I want to group a list of key value pairs by key, then to group all the keys which have the same list of value.



Here is an exemple



| Key | Value |
|:----|------:|
| 1 | A |
| 1 | B |
| 1 | C |
| 2 | A |
| 2 | B |
| 3 | A |


Here is the result I would like to have



| Keys | Values |
|:-----|-------:|
| 1,2 | A,B |
| 1 | C |
| 3 | A |


I started with a classic GroupBy, but I am stuck in the second step to group the keys



var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("1", "C"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;
var groupingFirstStep = pairs.GroupBy(p => p.Key);

| Key | Values |
|:-----|-------:|
| 1 | A,B,C |
| 2 | A,B |
| 3 | A |


Other examples: on the left the input, on the right the expected result



| Key | Value | | Keys | Values |
|:----|------:| |:-----|-------:|
| 1 | A | | 1,2 | A,B |
| 1 | B |
| 2 | A |
| 2 | B |

| Key | Value | | Keys | Values |
|:----|------:| |:-----|-------:|
| 1 | A | | 1,2 | A,B |
| 1 | B | | 1 | C |
| 1 | C |
| 2 | A |
| 2 | B |

| Key | Value | | Keys | Values |
|:----|------:| |:-----|-------:|
| 1 | A | | 1,2 | A,B |
| 1 | B | | 1,3 | C |
| 1 | C | | 3 | A |
| 2 | A |
| 2 | B | Other possibility
| 3 | A | | 1,3 | A,C |
| 3 | C | | 1,2 | B |
| 2 | A |









share|improve this question



















  • 3





    Your grouping has not the same list of values - they just have some intersection of values - and 1,3 - A would be possible as well - same for 1,2 - A

    – Patrick Artner
    Mar 26 at 16:31







  • 2





    Why 1 and 2 should be grouped together? 1 has A, B and C, while 2 has A and B.

    – Michał Turczyn
    Mar 26 at 16:33






  • 1





    @MichałTurczyn Probably because they both have A and B, but then why is there not a group showing than 1, 2 and 3 have A in common?

    – vc 74
    Mar 26 at 16:36












  • @MichałTurczyn, my goal is to minimize the number of lines whatever the grouping keys

    – Nicolas
    Mar 26 at 16:37






  • 1





    @Nicolas, your last table has the same number of lines than the second one, so, why not stick with that one?

    – Magnetron
    Mar 26 at 16:41

















2















I want to group a list of key value pairs by key, then to group all the keys which have the same list of value.



Here is an exemple



| Key | Value |
|:----|------:|
| 1 | A |
| 1 | B |
| 1 | C |
| 2 | A |
| 2 | B |
| 3 | A |


Here is the result I would like to have



| Keys | Values |
|:-----|-------:|
| 1,2 | A,B |
| 1 | C |
| 3 | A |


I started with a classic GroupBy, but I am stuck in the second step to group the keys



var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("1", "C"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;
var groupingFirstStep = pairs.GroupBy(p => p.Key);

| Key | Values |
|:-----|-------:|
| 1 | A,B,C |
| 2 | A,B |
| 3 | A |


Other examples: on the left the input, on the right the expected result



| Key | Value | | Keys | Values |
|:----|------:| |:-----|-------:|
| 1 | A | | 1,2 | A,B |
| 1 | B |
| 2 | A |
| 2 | B |

| Key | Value | | Keys | Values |
|:----|------:| |:-----|-------:|
| 1 | A | | 1,2 | A,B |
| 1 | B | | 1 | C |
| 1 | C |
| 2 | A |
| 2 | B |

| Key | Value | | Keys | Values |
|:----|------:| |:-----|-------:|
| 1 | A | | 1,2 | A,B |
| 1 | B | | 1,3 | C |
| 1 | C | | 3 | A |
| 2 | A |
| 2 | B | Other possibility
| 3 | A | | 1,3 | A,C |
| 3 | C | | 1,2 | B |
| 2 | A |









share|improve this question



















  • 3





    Your grouping has not the same list of values - they just have some intersection of values - and 1,3 - A would be possible as well - same for 1,2 - A

    – Patrick Artner
    Mar 26 at 16:31







  • 2





    Why 1 and 2 should be grouped together? 1 has A, B and C, while 2 has A and B.

    – Michał Turczyn
    Mar 26 at 16:33






  • 1





    @MichałTurczyn Probably because they both have A and B, but then why is there not a group showing than 1, 2 and 3 have A in common?

    – vc 74
    Mar 26 at 16:36












  • @MichałTurczyn, my goal is to minimize the number of lines whatever the grouping keys

    – Nicolas
    Mar 26 at 16:37






  • 1





    @Nicolas, your last table has the same number of lines than the second one, so, why not stick with that one?

    – Magnetron
    Mar 26 at 16:41













2












2








2


0






I want to group a list of key value pairs by key, then to group all the keys which have the same list of value.



Here is an exemple



| Key | Value |
|:----|------:|
| 1 | A |
| 1 | B |
| 1 | C |
| 2 | A |
| 2 | B |
| 3 | A |


Here is the result I would like to have



| Keys | Values |
|:-----|-------:|
| 1,2 | A,B |
| 1 | C |
| 3 | A |


I started with a classic GroupBy, but I am stuck in the second step to group the keys



var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("1", "C"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;
var groupingFirstStep = pairs.GroupBy(p => p.Key);

| Key | Values |
|:-----|-------:|
| 1 | A,B,C |
| 2 | A,B |
| 3 | A |


Other examples: on the left the input, on the right the expected result



| Key | Value | | Keys | Values |
|:----|------:| |:-----|-------:|
| 1 | A | | 1,2 | A,B |
| 1 | B |
| 2 | A |
| 2 | B |

| Key | Value | | Keys | Values |
|:----|------:| |:-----|-------:|
| 1 | A | | 1,2 | A,B |
| 1 | B | | 1 | C |
| 1 | C |
| 2 | A |
| 2 | B |

| Key | Value | | Keys | Values |
|:----|------:| |:-----|-------:|
| 1 | A | | 1,2 | A,B |
| 1 | B | | 1,3 | C |
| 1 | C | | 3 | A |
| 2 | A |
| 2 | B | Other possibility
| 3 | A | | 1,3 | A,C |
| 3 | C | | 1,2 | B |
| 2 | A |









share|improve this question
















I want to group a list of key value pairs by key, then to group all the keys which have the same list of value.



Here is an exemple



| Key | Value |
|:----|------:|
| 1 | A |
| 1 | B |
| 1 | C |
| 2 | A |
| 2 | B |
| 3 | A |


Here is the result I would like to have



| Keys | Values |
|:-----|-------:|
| 1,2 | A,B |
| 1 | C |
| 3 | A |


I started with a classic GroupBy, but I am stuck in the second step to group the keys



var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("1", "C"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;
var groupingFirstStep = pairs.GroupBy(p => p.Key);

| Key | Values |
|:-----|-------:|
| 1 | A,B,C |
| 2 | A,B |
| 3 | A |


Other examples: on the left the input, on the right the expected result



| Key | Value | | Keys | Values |
|:----|------:| |:-----|-------:|
| 1 | A | | 1,2 | A,B |
| 1 | B |
| 2 | A |
| 2 | B |

| Key | Value | | Keys | Values |
|:----|------:| |:-----|-------:|
| 1 | A | | 1,2 | A,B |
| 1 | B | | 1 | C |
| 1 | C |
| 2 | A |
| 2 | B |

| Key | Value | | Keys | Values |
|:----|------:| |:-----|-------:|
| 1 | A | | 1,2 | A,B |
| 1 | B | | 1,3 | C |
| 1 | C | | 3 | A |
| 2 | A |
| 2 | B | Other possibility
| 3 | A | | 1,3 | A,C |
| 3 | C | | 1,2 | B |
| 2 | A |






c# linq grouping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 9:14







Nicolas

















asked Mar 26 at 16:29









NicolasNicolas

4,5954 gold badges24 silver badges42 bronze badges




4,5954 gold badges24 silver badges42 bronze badges







  • 3





    Your grouping has not the same list of values - they just have some intersection of values - and 1,3 - A would be possible as well - same for 1,2 - A

    – Patrick Artner
    Mar 26 at 16:31







  • 2





    Why 1 and 2 should be grouped together? 1 has A, B and C, while 2 has A and B.

    – Michał Turczyn
    Mar 26 at 16:33






  • 1





    @MichałTurczyn Probably because they both have A and B, but then why is there not a group showing than 1, 2 and 3 have A in common?

    – vc 74
    Mar 26 at 16:36












  • @MichałTurczyn, my goal is to minimize the number of lines whatever the grouping keys

    – Nicolas
    Mar 26 at 16:37






  • 1





    @Nicolas, your last table has the same number of lines than the second one, so, why not stick with that one?

    – Magnetron
    Mar 26 at 16:41












  • 3





    Your grouping has not the same list of values - they just have some intersection of values - and 1,3 - A would be possible as well - same for 1,2 - A

    – Patrick Artner
    Mar 26 at 16:31







  • 2





    Why 1 and 2 should be grouped together? 1 has A, B and C, while 2 has A and B.

    – Michał Turczyn
    Mar 26 at 16:33






  • 1





    @MichałTurczyn Probably because they both have A and B, but then why is there not a group showing than 1, 2 and 3 have A in common?

    – vc 74
    Mar 26 at 16:36












  • @MichałTurczyn, my goal is to minimize the number of lines whatever the grouping keys

    – Nicolas
    Mar 26 at 16:37






  • 1





    @Nicolas, your last table has the same number of lines than the second one, so, why not stick with that one?

    – Magnetron
    Mar 26 at 16:41







3




3





Your grouping has not the same list of values - they just have some intersection of values - and 1,3 - A would be possible as well - same for 1,2 - A

– Patrick Artner
Mar 26 at 16:31






Your grouping has not the same list of values - they just have some intersection of values - and 1,3 - A would be possible as well - same for 1,2 - A

– Patrick Artner
Mar 26 at 16:31





2




2





Why 1 and 2 should be grouped together? 1 has A, B and C, while 2 has A and B.

– Michał Turczyn
Mar 26 at 16:33





Why 1 and 2 should be grouped together? 1 has A, B and C, while 2 has A and B.

– Michał Turczyn
Mar 26 at 16:33




1




1





@MichałTurczyn Probably because they both have A and B, but then why is there not a group showing than 1, 2 and 3 have A in common?

– vc 74
Mar 26 at 16:36






@MichałTurczyn Probably because they both have A and B, but then why is there not a group showing than 1, 2 and 3 have A in common?

– vc 74
Mar 26 at 16:36














@MichałTurczyn, my goal is to minimize the number of lines whatever the grouping keys

– Nicolas
Mar 26 at 16:37





@MichałTurczyn, my goal is to minimize the number of lines whatever the grouping keys

– Nicolas
Mar 26 at 16:37




1




1





@Nicolas, your last table has the same number of lines than the second one, so, why not stick with that one?

– Magnetron
Mar 26 at 16:41





@Nicolas, your last table has the same number of lines than the second one, so, why not stick with that one?

– Magnetron
Mar 26 at 16:41












2 Answers
2






active

oldest

votes


















1














I think this is what you need. I omitted KeyValuePair with C, because I assumed that you would like to know how to group keys when the value (in this case A,B) is the same:



var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;
var grouping = pairs.GroupBy(p => p.Key)
.GroupBy(g => string.Join(",", g.Select(kvp => kvp.Value)));





share|improve this answer

























  • It doesn't give the second line 1 | C

    – Magnetron
    Mar 26 at 16:42












  • If you omit the C you simplify too much, reintegrate it and you will see it doesn't work.

    – Nicolas
    Mar 26 at 16:56











  • After spending time looking for a solution, it appears yours is the best I saw: group a second time by the list of values. So it only groups the exact match of values, but it is better than nothing. Thanks.

    – Nicolas
    Apr 1 at 16:08


















0














You can use this overload of .GroupBy to project the values out of it.



Something like this:



static void Main(string[] args)

var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("1", "C"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;

IEnumerable<string> groupings = pairs.GroupBy(x => x.Key, x => x.Value, (x, y) => $"xt


Outputting:



1 | A,B,C
2 | A,B
3 | A





share|improve this answer

























  • Thanks for your answer. In the results I want 1 and 2 to be grouped because they have both A and B.

    – Nicolas
    Mar 29 at 14:17













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55361996%2fgroupby-a-list-of-key-value-pairs-then-regroup-the-result%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I think this is what you need. I omitted KeyValuePair with C, because I assumed that you would like to know how to group keys when the value (in this case A,B) is the same:



var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;
var grouping = pairs.GroupBy(p => p.Key)
.GroupBy(g => string.Join(",", g.Select(kvp => kvp.Value)));





share|improve this answer

























  • It doesn't give the second line 1 | C

    – Magnetron
    Mar 26 at 16:42












  • If you omit the C you simplify too much, reintegrate it and you will see it doesn't work.

    – Nicolas
    Mar 26 at 16:56











  • After spending time looking for a solution, it appears yours is the best I saw: group a second time by the list of values. So it only groups the exact match of values, but it is better than nothing. Thanks.

    – Nicolas
    Apr 1 at 16:08















1














I think this is what you need. I omitted KeyValuePair with C, because I assumed that you would like to know how to group keys when the value (in this case A,B) is the same:



var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;
var grouping = pairs.GroupBy(p => p.Key)
.GroupBy(g => string.Join(",", g.Select(kvp => kvp.Value)));





share|improve this answer

























  • It doesn't give the second line 1 | C

    – Magnetron
    Mar 26 at 16:42












  • If you omit the C you simplify too much, reintegrate it and you will see it doesn't work.

    – Nicolas
    Mar 26 at 16:56











  • After spending time looking for a solution, it appears yours is the best I saw: group a second time by the list of values. So it only groups the exact match of values, but it is better than nothing. Thanks.

    – Nicolas
    Apr 1 at 16:08













1












1








1







I think this is what you need. I omitted KeyValuePair with C, because I assumed that you would like to know how to group keys when the value (in this case A,B) is the same:



var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;
var grouping = pairs.GroupBy(p => p.Key)
.GroupBy(g => string.Join(",", g.Select(kvp => kvp.Value)));





share|improve this answer















I think this is what you need. I omitted KeyValuePair with C, because I assumed that you would like to know how to group keys when the value (in this case A,B) is the same:



var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;
var grouping = pairs.GroupBy(p => p.Key)
.GroupBy(g => string.Join(",", g.Select(kvp => kvp.Value)));






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 26 at 16:43

























answered Mar 26 at 16:41









Michał TurczynMichał Turczyn

19.5k13 gold badges24 silver badges42 bronze badges




19.5k13 gold badges24 silver badges42 bronze badges












  • It doesn't give the second line 1 | C

    – Magnetron
    Mar 26 at 16:42












  • If you omit the C you simplify too much, reintegrate it and you will see it doesn't work.

    – Nicolas
    Mar 26 at 16:56











  • After spending time looking for a solution, it appears yours is the best I saw: group a second time by the list of values. So it only groups the exact match of values, but it is better than nothing. Thanks.

    – Nicolas
    Apr 1 at 16:08

















  • It doesn't give the second line 1 | C

    – Magnetron
    Mar 26 at 16:42












  • If you omit the C you simplify too much, reintegrate it and you will see it doesn't work.

    – Nicolas
    Mar 26 at 16:56











  • After spending time looking for a solution, it appears yours is the best I saw: group a second time by the list of values. So it only groups the exact match of values, but it is better than nothing. Thanks.

    – Nicolas
    Apr 1 at 16:08
















It doesn't give the second line 1 | C

– Magnetron
Mar 26 at 16:42






It doesn't give the second line 1 | C

– Magnetron
Mar 26 at 16:42














If you omit the C you simplify too much, reintegrate it and you will see it doesn't work.

– Nicolas
Mar 26 at 16:56





If you omit the C you simplify too much, reintegrate it and you will see it doesn't work.

– Nicolas
Mar 26 at 16:56













After spending time looking for a solution, it appears yours is the best I saw: group a second time by the list of values. So it only groups the exact match of values, but it is better than nothing. Thanks.

– Nicolas
Apr 1 at 16:08





After spending time looking for a solution, it appears yours is the best I saw: group a second time by the list of values. So it only groups the exact match of values, but it is better than nothing. Thanks.

– Nicolas
Apr 1 at 16:08













0














You can use this overload of .GroupBy to project the values out of it.



Something like this:



static void Main(string[] args)

var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("1", "C"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;

IEnumerable<string> groupings = pairs.GroupBy(x => x.Key, x => x.Value, (x, y) => $"xt


Outputting:



1 | A,B,C
2 | A,B
3 | A





share|improve this answer

























  • Thanks for your answer. In the results I want 1 and 2 to be grouped because they have both A and B.

    – Nicolas
    Mar 29 at 14:17















0














You can use this overload of .GroupBy to project the values out of it.



Something like this:



static void Main(string[] args)

var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("1", "C"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;

IEnumerable<string> groupings = pairs.GroupBy(x => x.Key, x => x.Value, (x, y) => $"xt


Outputting:



1 | A,B,C
2 | A,B
3 | A





share|improve this answer

























  • Thanks for your answer. In the results I want 1 and 2 to be grouped because they have both A and B.

    – Nicolas
    Mar 29 at 14:17













0












0








0







You can use this overload of .GroupBy to project the values out of it.



Something like this:



static void Main(string[] args)

var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("1", "C"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;

IEnumerable<string> groupings = pairs.GroupBy(x => x.Key, x => x.Value, (x, y) => $"xt


Outputting:



1 | A,B,C
2 | A,B
3 | A





share|improve this answer















You can use this overload of .GroupBy to project the values out of it.



Something like this:



static void Main(string[] args)

var pairs = new List<KeyValuePair<string, string>>

new KeyValuePair<string, string>("1", "A"),
new KeyValuePair<string, string>("1", "B"),
new KeyValuePair<string, string>("1", "C"),
new KeyValuePair<string, string>("2", "A"),
new KeyValuePair<string, string>("2", "B"),
new KeyValuePair<string, string>("3", "A")
;

IEnumerable<string> groupings = pairs.GroupBy(x => x.Key, x => x.Value, (x, y) => $"xt


Outputting:



1 | A,B,C
2 | A,B
3 | A






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 29 at 13:23

























answered Mar 29 at 12:49









Kunal MukherjeeKunal Mukherjee

3,3093 gold badges12 silver badges31 bronze badges




3,3093 gold badges12 silver badges31 bronze badges












  • Thanks for your answer. In the results I want 1 and 2 to be grouped because they have both A and B.

    – Nicolas
    Mar 29 at 14:17

















  • Thanks for your answer. In the results I want 1 and 2 to be grouped because they have both A and B.

    – Nicolas
    Mar 29 at 14:17
















Thanks for your answer. In the results I want 1 and 2 to be grouped because they have both A and B.

– Nicolas
Mar 29 at 14:17





Thanks for your answer. In the results I want 1 and 2 to be grouped because they have both A and B.

– Nicolas
Mar 29 at 14:17

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55361996%2fgroupby-a-list-of-key-value-pairs-then-regroup-the-result%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript