C# group by list then SUM
How to group by then sum inside the list
below is my sample code:
List<BrandType> brandTypeList = new List<BrandType>();
BrandType brandTypeClass = new BrandType();
brandTypeClass.Amount = 100;
brandTypeClass.Count = 50;
brandTypeClass.Category = "Fish";
brandTypeList.Add(brandTypeClass);
BrandType brandTypeClass2 = new BrandType();
brandTypeClass2.Amount = 100;
brandTypeClass2.Count = 50;
brandTypeClass2.Category = "Fish";
brandTypeList.Add(brandTypeClass2);
BrandType brandTypeClass3 = new BrandType();
brandTypeClass3.Amount = 100;
brandTypeClass3.Count = 50;
brandTypeClass3.Category = "Pork";
brandTypeList.Add(brandTypeClass3);
brandTypeList.GroupBy(x => new { x.Category }).Select
(x => new { Category = x.Key.Category,
Amount = x.Sum(z => z.Amount),
Count = x.Sum(z => z.Count) });
Here's what it looks like
[0] {Amount = 100, Category = "Pork", Count = 50}
[1] {Amount = 100, Category = "Fish", Count = 50}
[2] {Amount = 100, Category = "Fish", Count = 50}
How can I SUM the amout and count then group by Category?
I want the result to be
Amount = 200, Category = "Fish", Count = 100
Amount = 100, Category = "Pork" Count = 50
c# list linq
add a comment |
How to group by then sum inside the list
below is my sample code:
List<BrandType> brandTypeList = new List<BrandType>();
BrandType brandTypeClass = new BrandType();
brandTypeClass.Amount = 100;
brandTypeClass.Count = 50;
brandTypeClass.Category = "Fish";
brandTypeList.Add(brandTypeClass);
BrandType brandTypeClass2 = new BrandType();
brandTypeClass2.Amount = 100;
brandTypeClass2.Count = 50;
brandTypeClass2.Category = "Fish";
brandTypeList.Add(brandTypeClass2);
BrandType brandTypeClass3 = new BrandType();
brandTypeClass3.Amount = 100;
brandTypeClass3.Count = 50;
brandTypeClass3.Category = "Pork";
brandTypeList.Add(brandTypeClass3);
brandTypeList.GroupBy(x => new { x.Category }).Select
(x => new { Category = x.Key.Category,
Amount = x.Sum(z => z.Amount),
Count = x.Sum(z => z.Count) });
Here's what it looks like
[0] {Amount = 100, Category = "Pork", Count = 50}
[1] {Amount = 100, Category = "Fish", Count = 50}
[2] {Amount = 100, Category = "Fish", Count = 50}
How can I SUM the amout and count then group by Category?
I want the result to be
Amount = 200, Category = "Fish", Count = 100
Amount = 100, Category = "Pork" Count = 50
c# list linq
1
stackoverflow.com/a/4351889/3583859 This link might be helpful
– Vijay Kumbhoje
Nov 23 '18 at 3:03
1
The primary cause of your issue is x => new { x.Category } inside the GroupBy call. It should just be GroupBy(x => x.Category)
– Brian Driscoll
Nov 23 '18 at 3:06
add a comment |
How to group by then sum inside the list
below is my sample code:
List<BrandType> brandTypeList = new List<BrandType>();
BrandType brandTypeClass = new BrandType();
brandTypeClass.Amount = 100;
brandTypeClass.Count = 50;
brandTypeClass.Category = "Fish";
brandTypeList.Add(brandTypeClass);
BrandType brandTypeClass2 = new BrandType();
brandTypeClass2.Amount = 100;
brandTypeClass2.Count = 50;
brandTypeClass2.Category = "Fish";
brandTypeList.Add(brandTypeClass2);
BrandType brandTypeClass3 = new BrandType();
brandTypeClass3.Amount = 100;
brandTypeClass3.Count = 50;
brandTypeClass3.Category = "Pork";
brandTypeList.Add(brandTypeClass3);
brandTypeList.GroupBy(x => new { x.Category }).Select
(x => new { Category = x.Key.Category,
Amount = x.Sum(z => z.Amount),
Count = x.Sum(z => z.Count) });
Here's what it looks like
[0] {Amount = 100, Category = "Pork", Count = 50}
[1] {Amount = 100, Category = "Fish", Count = 50}
[2] {Amount = 100, Category = "Fish", Count = 50}
How can I SUM the amout and count then group by Category?
I want the result to be
Amount = 200, Category = "Fish", Count = 100
Amount = 100, Category = "Pork" Count = 50
c# list linq
How to group by then sum inside the list
below is my sample code:
List<BrandType> brandTypeList = new List<BrandType>();
BrandType brandTypeClass = new BrandType();
brandTypeClass.Amount = 100;
brandTypeClass.Count = 50;
brandTypeClass.Category = "Fish";
brandTypeList.Add(brandTypeClass);
BrandType brandTypeClass2 = new BrandType();
brandTypeClass2.Amount = 100;
brandTypeClass2.Count = 50;
brandTypeClass2.Category = "Fish";
brandTypeList.Add(brandTypeClass2);
BrandType brandTypeClass3 = new BrandType();
brandTypeClass3.Amount = 100;
brandTypeClass3.Count = 50;
brandTypeClass3.Category = "Pork";
brandTypeList.Add(brandTypeClass3);
brandTypeList.GroupBy(x => new { x.Category }).Select
(x => new { Category = x.Key.Category,
Amount = x.Sum(z => z.Amount),
Count = x.Sum(z => z.Count) });
Here's what it looks like
[0] {Amount = 100, Category = "Pork", Count = 50}
[1] {Amount = 100, Category = "Fish", Count = 50}
[2] {Amount = 100, Category = "Fish", Count = 50}
How can I SUM the amout and count then group by Category?
I want the result to be
Amount = 200, Category = "Fish", Count = 100
Amount = 100, Category = "Pork" Count = 50
c# list linq
c# list linq
asked Nov 23 '18 at 2:50
beginnerlaravelvuebeginnerlaravelvue
528
528
1
stackoverflow.com/a/4351889/3583859 This link might be helpful
– Vijay Kumbhoje
Nov 23 '18 at 3:03
1
The primary cause of your issue is x => new { x.Category } inside the GroupBy call. It should just be GroupBy(x => x.Category)
– Brian Driscoll
Nov 23 '18 at 3:06
add a comment |
1
stackoverflow.com/a/4351889/3583859 This link might be helpful
– Vijay Kumbhoje
Nov 23 '18 at 3:03
1
The primary cause of your issue is x => new { x.Category } inside the GroupBy call. It should just be GroupBy(x => x.Category)
– Brian Driscoll
Nov 23 '18 at 3:06
1
1
stackoverflow.com/a/4351889/3583859 This link might be helpful
– Vijay Kumbhoje
Nov 23 '18 at 3:03
stackoverflow.com/a/4351889/3583859 This link might be helpful
– Vijay Kumbhoje
Nov 23 '18 at 3:03
1
1
The primary cause of your issue is x => new { x.Category } inside the GroupBy call. It should just be GroupBy(x => x.Category)
– Brian Driscoll
Nov 23 '18 at 3:06
The primary cause of your issue is x => new { x.Category } inside the GroupBy call. It should just be GroupBy(x => x.Category)
– Brian Driscoll
Nov 23 '18 at 3:06
add a comment |
1 Answer
1
active
oldest
votes
The following code snippet will work for you:
var result = from brand in brandTypeList
group brand by brand.Category into grp
select new
{
Category = grp.Key,
Amount = grp.Sum(z => z.Amount),
Count = grp.Sum(z => z.Count)
};
This one also works fine:
var result = brandTypeList.GroupBy(x => x.Category).Select
(y => new {
Category = y.Key,
Amount = y.Sum(z => z.Amount),
Count = y.Sum(z => z.Count)
});
It turns out that your code also works fine, probably the problem is that you expect list to be modified inplace, but linq does not produce side effects and new IEnumerable will be created and you need to save results to variable.
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%2f53440109%2fc-sharp-group-by-list-then-sum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The following code snippet will work for you:
var result = from brand in brandTypeList
group brand by brand.Category into grp
select new
{
Category = grp.Key,
Amount = grp.Sum(z => z.Amount),
Count = grp.Sum(z => z.Count)
};
This one also works fine:
var result = brandTypeList.GroupBy(x => x.Category).Select
(y => new {
Category = y.Key,
Amount = y.Sum(z => z.Amount),
Count = y.Sum(z => z.Count)
});
It turns out that your code also works fine, probably the problem is that you expect list to be modified inplace, but linq does not produce side effects and new IEnumerable will be created and you need to save results to variable.
add a comment |
The following code snippet will work for you:
var result = from brand in brandTypeList
group brand by brand.Category into grp
select new
{
Category = grp.Key,
Amount = grp.Sum(z => z.Amount),
Count = grp.Sum(z => z.Count)
};
This one also works fine:
var result = brandTypeList.GroupBy(x => x.Category).Select
(y => new {
Category = y.Key,
Amount = y.Sum(z => z.Amount),
Count = y.Sum(z => z.Count)
});
It turns out that your code also works fine, probably the problem is that you expect list to be modified inplace, but linq does not produce side effects and new IEnumerable will be created and you need to save results to variable.
add a comment |
The following code snippet will work for you:
var result = from brand in brandTypeList
group brand by brand.Category into grp
select new
{
Category = grp.Key,
Amount = grp.Sum(z => z.Amount),
Count = grp.Sum(z => z.Count)
};
This one also works fine:
var result = brandTypeList.GroupBy(x => x.Category).Select
(y => new {
Category = y.Key,
Amount = y.Sum(z => z.Amount),
Count = y.Sum(z => z.Count)
});
It turns out that your code also works fine, probably the problem is that you expect list to be modified inplace, but linq does not produce side effects and new IEnumerable will be created and you need to save results to variable.
The following code snippet will work for you:
var result = from brand in brandTypeList
group brand by brand.Category into grp
select new
{
Category = grp.Key,
Amount = grp.Sum(z => z.Amount),
Count = grp.Sum(z => z.Count)
};
This one also works fine:
var result = brandTypeList.GroupBy(x => x.Category).Select
(y => new {
Category = y.Key,
Amount = y.Sum(z => z.Amount),
Count = y.Sum(z => z.Count)
});
It turns out that your code also works fine, probably the problem is that you expect list to be modified inplace, but linq does not produce side effects and new IEnumerable will be created and you need to save results to variable.
edited Nov 23 '18 at 3:06
answered Nov 23 '18 at 2:58
Access DeniedAccess Denied
5,11921844
5,11921844
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%2f53440109%2fc-sharp-group-by-list-then-sum%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
1
stackoverflow.com/a/4351889/3583859 This link might be helpful
– Vijay Kumbhoje
Nov 23 '18 at 3:03
1
The primary cause of your issue is x => new { x.Category } inside the GroupBy call. It should just be GroupBy(x => x.Category)
– Brian Driscoll
Nov 23 '18 at 3:06