Nested GroupBy in C#
Having an object with this structure:
public class GroupedObject
{
public int id { get; set; }
public string name { get; set; }
public int value { get; set; }
public string color { get; set; }
}
Here it is grouped by one of its attributes, id
.
var myObj = someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id)
.ToDictionary(a => a.Key, a => a.Select(o => o).ToList());
The structure is:
id - id, name, value, color
id - id, name, value, color
id - id, name, value, color
What I want to do is to group it again by name
, to have a nested structure like this:
id - name - id, name, value, color
- name - id, name, value, color
id - name - id, name, value, color
- name - id, name, value, color
- name - id, name, value, color
- name - id, name, value, color
id - name - id, name, value, color
- name - id, name, value, color
- name - id, name, value, color
Trying to use GroupBy
twice didn't work. Neither by transforming it with ToList
and grouping after that.
var myObj = someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id).GroupBy(o => o.name)
.ToDictionary(a => a.Key, a => a.Select(o => o).ToList());
c# dictionary group-by tolist todictionary
add a comment |
Having an object with this structure:
public class GroupedObject
{
public int id { get; set; }
public string name { get; set; }
public int value { get; set; }
public string color { get; set; }
}
Here it is grouped by one of its attributes, id
.
var myObj = someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id)
.ToDictionary(a => a.Key, a => a.Select(o => o).ToList());
The structure is:
id - id, name, value, color
id - id, name, value, color
id - id, name, value, color
What I want to do is to group it again by name
, to have a nested structure like this:
id - name - id, name, value, color
- name - id, name, value, color
id - name - id, name, value, color
- name - id, name, value, color
- name - id, name, value, color
- name - id, name, value, color
id - name - id, name, value, color
- name - id, name, value, color
- name - id, name, value, color
Trying to use GroupBy
twice didn't work. Neither by transforming it with ToList
and grouping after that.
var myObj = someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id).GroupBy(o => o.name)
.ToDictionary(a => a.Key, a => a.Select(o => o).ToList());
c# dictionary group-by tolist todictionary
Surely you have to ToDictionary the first GroupBy and then instead of doing a.Select... in the ToDictionary, you'd have a.GroupBy and chain an entire ToDictionary to that with another .Select.
– Mark Rabjohn
Nov 20 '18 at 12:39
Your desired end result should look like this?Dictionary<int, Dictionay<string, List<GroupedObject>>
- where theint
key of the outerDictionary
is theid
and thestring
key of the innerDictionary
is thename
?
– Rand Random
Nov 20 '18 at 12:51
@RandRandom Yes
– Leo Messi
Nov 20 '18 at 14:55
add a comment |
Having an object with this structure:
public class GroupedObject
{
public int id { get; set; }
public string name { get; set; }
public int value { get; set; }
public string color { get; set; }
}
Here it is grouped by one of its attributes, id
.
var myObj = someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id)
.ToDictionary(a => a.Key, a => a.Select(o => o).ToList());
The structure is:
id - id, name, value, color
id - id, name, value, color
id - id, name, value, color
What I want to do is to group it again by name
, to have a nested structure like this:
id - name - id, name, value, color
- name - id, name, value, color
id - name - id, name, value, color
- name - id, name, value, color
- name - id, name, value, color
- name - id, name, value, color
id - name - id, name, value, color
- name - id, name, value, color
- name - id, name, value, color
Trying to use GroupBy
twice didn't work. Neither by transforming it with ToList
and grouping after that.
var myObj = someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id).GroupBy(o => o.name)
.ToDictionary(a => a.Key, a => a.Select(o => o).ToList());
c# dictionary group-by tolist todictionary
Having an object with this structure:
public class GroupedObject
{
public int id { get; set; }
public string name { get; set; }
public int value { get; set; }
public string color { get; set; }
}
Here it is grouped by one of its attributes, id
.
var myObj = someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id)
.ToDictionary(a => a.Key, a => a.Select(o => o).ToList());
The structure is:
id - id, name, value, color
id - id, name, value, color
id - id, name, value, color
What I want to do is to group it again by name
, to have a nested structure like this:
id - name - id, name, value, color
- name - id, name, value, color
id - name - id, name, value, color
- name - id, name, value, color
- name - id, name, value, color
- name - id, name, value, color
id - name - id, name, value, color
- name - id, name, value, color
- name - id, name, value, color
Trying to use GroupBy
twice didn't work. Neither by transforming it with ToList
and grouping after that.
var myObj = someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id).GroupBy(o => o.name)
.ToDictionary(a => a.Key, a => a.Select(o => o).ToList());
c# dictionary group-by tolist todictionary
c# dictionary group-by tolist todictionary
edited Nov 20 '18 at 15:00
Rand Random
2,94573163
2,94573163
asked Nov 20 '18 at 12:34


Leo MessiLeo Messi
586418
586418
Surely you have to ToDictionary the first GroupBy and then instead of doing a.Select... in the ToDictionary, you'd have a.GroupBy and chain an entire ToDictionary to that with another .Select.
– Mark Rabjohn
Nov 20 '18 at 12:39
Your desired end result should look like this?Dictionary<int, Dictionay<string, List<GroupedObject>>
- where theint
key of the outerDictionary
is theid
and thestring
key of the innerDictionary
is thename
?
– Rand Random
Nov 20 '18 at 12:51
@RandRandom Yes
– Leo Messi
Nov 20 '18 at 14:55
add a comment |
Surely you have to ToDictionary the first GroupBy and then instead of doing a.Select... in the ToDictionary, you'd have a.GroupBy and chain an entire ToDictionary to that with another .Select.
– Mark Rabjohn
Nov 20 '18 at 12:39
Your desired end result should look like this?Dictionary<int, Dictionay<string, List<GroupedObject>>
- where theint
key of the outerDictionary
is theid
and thestring
key of the innerDictionary
is thename
?
– Rand Random
Nov 20 '18 at 12:51
@RandRandom Yes
– Leo Messi
Nov 20 '18 at 14:55
Surely you have to ToDictionary the first GroupBy and then instead of doing a.Select... in the ToDictionary, you'd have a.GroupBy and chain an entire ToDictionary to that with another .Select.
– Mark Rabjohn
Nov 20 '18 at 12:39
Surely you have to ToDictionary the first GroupBy and then instead of doing a.Select... in the ToDictionary, you'd have a.GroupBy and chain an entire ToDictionary to that with another .Select.
– Mark Rabjohn
Nov 20 '18 at 12:39
Your desired end result should look like this?
Dictionary<int, Dictionay<string, List<GroupedObject>>
- where the int
key of the outer Dictionary
is the id
and the string
key of the inner Dictionary
is the name
?– Rand Random
Nov 20 '18 at 12:51
Your desired end result should look like this?
Dictionary<int, Dictionay<string, List<GroupedObject>>
- where the int
key of the outer Dictionary
is the id
and the string
key of the inner Dictionary
is the name
?– Rand Random
Nov 20 '18 at 12:51
@RandRandom Yes
– Leo Messi
Nov 20 '18 at 14:55
@RandRandom Yes
– Leo Messi
Nov 20 '18 at 14:55
add a comment |
2 Answers
2
active
oldest
votes
You were on the right track, however here is the LINQ to achieve two nested levels of grouping:
someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id)
.ToDictionary(
a => a.Key,
a => a.GroupBy(o => o.name).ToDictionary(o => o.Key, o => o));
This will result in object of type: Dictionary<int, Dictionary<string, IGrouping<string, GroupedObject>>>
Taken that into account, last IGrouping<string, GroupedObject>
type can be changed/projected into different type specifying suitable lamda inside nested ToDictionary
call:
e.g. Changing call into ToDictionary(o => o.Key, o => o.ToList())
results with Dictionary<int, Dictionary<string, List<GroupedObject>>>
1
Propably want to changeo => o
too => o.ToList()
to "resolve" the group and get the following type:Dictionary<int, Dictionary<string, List<GroupedObject>>
.. to late
– Rand Random
Nov 20 '18 at 12:58
@RandRandom I agree, that's why I've edited question with that example, I just wanted to show OP possibilities since I'm not sure what type he actually wants to have
– Darjan Bogdan
Nov 20 '18 at 12:59
add a comment |
Using comprehension syntax might be easier:
var myObj = from sr in someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
group sr by sr.id into g1
select new {
ID = g1.Key,
NamesGroup = from n in g1
group n by n.name into g2
select new {
Name = g2.Key,
...
}
};
Note: If you download then wonderful LINQPad from LINQPad.net, in Samples you would find a very nice nested grouping sample using Northwind sample database.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393106%2fnested-groupby-in-c-sharp%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
You were on the right track, however here is the LINQ to achieve two nested levels of grouping:
someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id)
.ToDictionary(
a => a.Key,
a => a.GroupBy(o => o.name).ToDictionary(o => o.Key, o => o));
This will result in object of type: Dictionary<int, Dictionary<string, IGrouping<string, GroupedObject>>>
Taken that into account, last IGrouping<string, GroupedObject>
type can be changed/projected into different type specifying suitable lamda inside nested ToDictionary
call:
e.g. Changing call into ToDictionary(o => o.Key, o => o.ToList())
results with Dictionary<int, Dictionary<string, List<GroupedObject>>>
1
Propably want to changeo => o
too => o.ToList()
to "resolve" the group and get the following type:Dictionary<int, Dictionary<string, List<GroupedObject>>
.. to late
– Rand Random
Nov 20 '18 at 12:58
@RandRandom I agree, that's why I've edited question with that example, I just wanted to show OP possibilities since I'm not sure what type he actually wants to have
– Darjan Bogdan
Nov 20 '18 at 12:59
add a comment |
You were on the right track, however here is the LINQ to achieve two nested levels of grouping:
someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id)
.ToDictionary(
a => a.Key,
a => a.GroupBy(o => o.name).ToDictionary(o => o.Key, o => o));
This will result in object of type: Dictionary<int, Dictionary<string, IGrouping<string, GroupedObject>>>
Taken that into account, last IGrouping<string, GroupedObject>
type can be changed/projected into different type specifying suitable lamda inside nested ToDictionary
call:
e.g. Changing call into ToDictionary(o => o.Key, o => o.ToList())
results with Dictionary<int, Dictionary<string, List<GroupedObject>>>
1
Propably want to changeo => o
too => o.ToList()
to "resolve" the group and get the following type:Dictionary<int, Dictionary<string, List<GroupedObject>>
.. to late
– Rand Random
Nov 20 '18 at 12:58
@RandRandom I agree, that's why I've edited question with that example, I just wanted to show OP possibilities since I'm not sure what type he actually wants to have
– Darjan Bogdan
Nov 20 '18 at 12:59
add a comment |
You were on the right track, however here is the LINQ to achieve two nested levels of grouping:
someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id)
.ToDictionary(
a => a.Key,
a => a.GroupBy(o => o.name).ToDictionary(o => o.Key, o => o));
This will result in object of type: Dictionary<int, Dictionary<string, IGrouping<string, GroupedObject>>>
Taken that into account, last IGrouping<string, GroupedObject>
type can be changed/projected into different type specifying suitable lamda inside nested ToDictionary
call:
e.g. Changing call into ToDictionary(o => o.Key, o => o.ToList())
results with Dictionary<int, Dictionary<string, List<GroupedObject>>>
You were on the right track, however here is the LINQ to achieve two nested levels of grouping:
someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
.GroupBy(o => o.id)
.ToDictionary(
a => a.Key,
a => a.GroupBy(o => o.name).ToDictionary(o => o.Key, o => o));
This will result in object of type: Dictionary<int, Dictionary<string, IGrouping<string, GroupedObject>>>
Taken that into account, last IGrouping<string, GroupedObject>
type can be changed/projected into different type specifying suitable lamda inside nested ToDictionary
call:
e.g. Changing call into ToDictionary(o => o.Key, o => o.ToList())
results with Dictionary<int, Dictionary<string, List<GroupedObject>>>
edited Nov 20 '18 at 14:10
answered Nov 20 '18 at 12:54
Darjan BogdanDarjan Bogdan
2,35111325
2,35111325
1
Propably want to changeo => o
too => o.ToList()
to "resolve" the group and get the following type:Dictionary<int, Dictionary<string, List<GroupedObject>>
.. to late
– Rand Random
Nov 20 '18 at 12:58
@RandRandom I agree, that's why I've edited question with that example, I just wanted to show OP possibilities since I'm not sure what type he actually wants to have
– Darjan Bogdan
Nov 20 '18 at 12:59
add a comment |
1
Propably want to changeo => o
too => o.ToList()
to "resolve" the group and get the following type:Dictionary<int, Dictionary<string, List<GroupedObject>>
.. to late
– Rand Random
Nov 20 '18 at 12:58
@RandRandom I agree, that's why I've edited question with that example, I just wanted to show OP possibilities since I'm not sure what type he actually wants to have
– Darjan Bogdan
Nov 20 '18 at 12:59
1
1
Propably want to change
o => o
to o => o.ToList()
to "resolve" the group and get the following type: Dictionary<int, Dictionary<string, List<GroupedObject>>
.. to late– Rand Random
Nov 20 '18 at 12:58
Propably want to change
o => o
to o => o.ToList()
to "resolve" the group and get the following type: Dictionary<int, Dictionary<string, List<GroupedObject>>
.. to late– Rand Random
Nov 20 '18 at 12:58
@RandRandom I agree, that's why I've edited question with that example, I just wanted to show OP possibilities since I'm not sure what type he actually wants to have
– Darjan Bogdan
Nov 20 '18 at 12:59
@RandRandom I agree, that's why I've edited question with that example, I just wanted to show OP possibilities since I'm not sure what type he actually wants to have
– Darjan Bogdan
Nov 20 '18 at 12:59
add a comment |
Using comprehension syntax might be easier:
var myObj = from sr in someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
group sr by sr.id into g1
select new {
ID = g1.Key,
NamesGroup = from n in g1
group n by n.name into g2
select new {
Name = g2.Key,
...
}
};
Note: If you download then wonderful LINQPad from LINQPad.net, in Samples you would find a very nice nested grouping sample using Northwind sample database.
add a comment |
Using comprehension syntax might be easier:
var myObj = from sr in someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
group sr by sr.id into g1
select new {
ID = g1.Key,
NamesGroup = from n in g1
group n by n.name into g2
select new {
Name = g2.Key,
...
}
};
Note: If you download then wonderful LINQPad from LINQPad.net, in Samples you would find a very nice nested grouping sample using Northwind sample database.
add a comment |
Using comprehension syntax might be easier:
var myObj = from sr in someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
group sr by sr.id into g1
select new {
ID = g1.Key,
NamesGroup = from n in g1
group n by n.name into g2
select new {
Name = g2.Key,
...
}
};
Note: If you download then wonderful LINQPad from LINQPad.net, in Samples you would find a very nice nested grouping sample using Northwind sample database.
Using comprehension syntax might be easier:
var myObj = from sr in someResponse
.Select(d => new GroupedObject
{
id = d.id,
name = d.name,
value = d.value,
color = d.color
})
group sr by sr.id into g1
select new {
ID = g1.Key,
NamesGroup = from n in g1
group n by n.name into g2
select new {
Name = g2.Key,
...
}
};
Note: If you download then wonderful LINQPad from LINQPad.net, in Samples you would find a very nice nested grouping sample using Northwind sample database.
answered Nov 20 '18 at 12:58
Cetin BasozCetin Basoz
11k11527
11k11527
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393106%2fnested-groupby-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Surely you have to ToDictionary the first GroupBy and then instead of doing a.Select... in the ToDictionary, you'd have a.GroupBy and chain an entire ToDictionary to that with another .Select.
– Mark Rabjohn
Nov 20 '18 at 12:39
Your desired end result should look like this?
Dictionary<int, Dictionay<string, List<GroupedObject>>
- where theint
key of the outerDictionary
is theid
and thestring
key of the innerDictionary
is thename
?– Rand Random
Nov 20 '18 at 12:51
@RandRandom Yes
– Leo Messi
Nov 20 '18 at 14:55