LINQ - Group By and SumLINQ query on a DataTableMultiple “order by” in LINQGroup By Multiple ColumnsWhen to use .First and when to use .FirstOrDefault with LINQ?What is the Java equivalent for LINQ?LINQ Aggregate algorithm explainedLINQ with groupby and countGroup by in LINQLinq query to sum by groupHow to return an object with the number of grouped rows plus the group by field value?
Why is Madam Hooch not a professor?
Pronunciation of "œuf" in "deux œufs kinder" and "bœuf "in "deux bœufs bourguignons" as an exception to silent /f/ in the plural
The difference between Rad1 and Rfd1
SPI Waveform on Raspberry Pi Not clean and I'm wondering why
I played my first (rapid) tournament recently and I wanted to calculate my ELO
Going to get married soon, should I do it on Dec 31 or Jan 1?
Zombie Diet, why humans
What is the best delay to use between characters sent to the serial port
Did Chinese school textbook maps (c. 1951) "depict China as stretching even into the central Asian republics"?
Set vertical spacing between two particular items
Why won't the ground take my seed?
How can I convince my reader that I will not use a certain trope?
Generate and graph the Recamán Sequence
Anagram Within an Anagram!
“Faire” being used to mean “avoir l’air”?
Should I hide continue button until tasks are completed?
“Transitive verb” + interrupter+ “object”?
How do I find and plot the intersection of these three surfaces?
Avoid bfseries from bolding pm in siunitx
How to convert object fill in to fine lines?
Procedurally generate regions on island
Do sudoku answers always have a single minimal clue set?
Why does the A-4 Skyhawk sit nose-up when on ground?
If a high rpm motor is run at lower rpm, will it produce more torque?
LINQ - Group By and Sum
LINQ query on a DataTableMultiple “order by” in LINQGroup By Multiple ColumnsWhen to use .First and when to use .FirstOrDefault with LINQ?What is the Java equivalent for LINQ?LINQ Aggregate algorithm explainedLINQ with groupby and countGroup by in LINQLinq query to sum by groupHow to return an object with the number of grouped rows plus the group by field value?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a DataTable
(CategoryUnsummarised
) which has three columns: Category
, DataSort
and Net
.
This is used to drive a report showing totals by category. The reporting application is Devexpress XtraReports. This is within their layout scripting.
This table has several rows of different Net for each Category. The DataSort column is only there to ensure that the category "Other" appears last in the list when sorted. It will only ever be 1 or 2.
An example of the data:
I am trying to create a new table which is a sum of Net grouped by Category and Datasort.
Desired data:
The below is what I have tried, based on other examples on StockOverflow, however it is giving me an error.
' Sets columns in CategorySummary table
CategorySummary = CategoryUnsummarised.clone()
' Now summarises the rows. Groups by Category and Datasort, and gives sum total.
Dim CatGroups = CategoryUnsummarised.AsEnumerable().
GroupBy(Function(row) New With
Key .Category = row.Field(Of String)("Category"),
Key .DataSort = row.Field(Of integer)("DataSort")
)
For Each GroupRow In CatGroups
CategorySummary.Rows.Add(GroupRow.Key.Category, GroupRow.Sum(Function(row) row.Field(Of decimal)("Net")), GroupRow.Key.DataSort)
Next
This is the error that I'm getting:
If I declare GroupRow as a variable, it allows the code, however then I get the following:
As far as I can see, the Linq code is valid.
.net vb.net linq
add a comment |
I have a DataTable
(CategoryUnsummarised
) which has three columns: Category
, DataSort
and Net
.
This is used to drive a report showing totals by category. The reporting application is Devexpress XtraReports. This is within their layout scripting.
This table has several rows of different Net for each Category. The DataSort column is only there to ensure that the category "Other" appears last in the list when sorted. It will only ever be 1 or 2.
An example of the data:
I am trying to create a new table which is a sum of Net grouped by Category and Datasort.
Desired data:
The below is what I have tried, based on other examples on StockOverflow, however it is giving me an error.
' Sets columns in CategorySummary table
CategorySummary = CategoryUnsummarised.clone()
' Now summarises the rows. Groups by Category and Datasort, and gives sum total.
Dim CatGroups = CategoryUnsummarised.AsEnumerable().
GroupBy(Function(row) New With
Key .Category = row.Field(Of String)("Category"),
Key .DataSort = row.Field(Of integer)("DataSort")
)
For Each GroupRow In CatGroups
CategorySummary.Rows.Add(GroupRow.Key.Category, GroupRow.Sum(Function(row) row.Field(Of decimal)("Net")), GroupRow.Key.DataSort)
Next
This is the error that I'm getting:
If I declare GroupRow as a variable, it allows the code, however then I get the following:
As far as I can see, the Linq code is valid.
.net vb.net linq
TurnOption Strict On
...
– Çöđěxěŕ
Mar 25 at 11:59
I've run your sample, and it works with or without option explicit/option strict. It's correct.
– reckface
Mar 25 at 13:35
Thanks for confirming that it's valid code. I can only assume that the XtraReports application does not like Linq queries within its scripting editor.
– OWSam
Mar 25 at 14:28
add a comment |
I have a DataTable
(CategoryUnsummarised
) which has three columns: Category
, DataSort
and Net
.
This is used to drive a report showing totals by category. The reporting application is Devexpress XtraReports. This is within their layout scripting.
This table has several rows of different Net for each Category. The DataSort column is only there to ensure that the category "Other" appears last in the list when sorted. It will only ever be 1 or 2.
An example of the data:
I am trying to create a new table which is a sum of Net grouped by Category and Datasort.
Desired data:
The below is what I have tried, based on other examples on StockOverflow, however it is giving me an error.
' Sets columns in CategorySummary table
CategorySummary = CategoryUnsummarised.clone()
' Now summarises the rows. Groups by Category and Datasort, and gives sum total.
Dim CatGroups = CategoryUnsummarised.AsEnumerable().
GroupBy(Function(row) New With
Key .Category = row.Field(Of String)("Category"),
Key .DataSort = row.Field(Of integer)("DataSort")
)
For Each GroupRow In CatGroups
CategorySummary.Rows.Add(GroupRow.Key.Category, GroupRow.Sum(Function(row) row.Field(Of decimal)("Net")), GroupRow.Key.DataSort)
Next
This is the error that I'm getting:
If I declare GroupRow as a variable, it allows the code, however then I get the following:
As far as I can see, the Linq code is valid.
.net vb.net linq
I have a DataTable
(CategoryUnsummarised
) which has three columns: Category
, DataSort
and Net
.
This is used to drive a report showing totals by category. The reporting application is Devexpress XtraReports. This is within their layout scripting.
This table has several rows of different Net for each Category. The DataSort column is only there to ensure that the category "Other" appears last in the list when sorted. It will only ever be 1 or 2.
An example of the data:
I am trying to create a new table which is a sum of Net grouped by Category and Datasort.
Desired data:
The below is what I have tried, based on other examples on StockOverflow, however it is giving me an error.
' Sets columns in CategorySummary table
CategorySummary = CategoryUnsummarised.clone()
' Now summarises the rows. Groups by Category and Datasort, and gives sum total.
Dim CatGroups = CategoryUnsummarised.AsEnumerable().
GroupBy(Function(row) New With
Key .Category = row.Field(Of String)("Category"),
Key .DataSort = row.Field(Of integer)("DataSort")
)
For Each GroupRow In CatGroups
CategorySummary.Rows.Add(GroupRow.Key.Category, GroupRow.Sum(Function(row) row.Field(Of decimal)("Net")), GroupRow.Key.DataSort)
Next
This is the error that I'm getting:
If I declare GroupRow as a variable, it allows the code, however then I get the following:
As far as I can see, the Linq code is valid.
.net vb.net linq
.net vb.net linq
edited Mar 25 at 13:25
Michał Turczyn
18.4k13 gold badges22 silver badges41 bronze badges
18.4k13 gold badges22 silver badges41 bronze badges
asked Mar 25 at 11:38
OWSamOWSam
3501 gold badge2 silver badges17 bronze badges
3501 gold badge2 silver badges17 bronze badges
TurnOption Strict On
...
– Çöđěxěŕ
Mar 25 at 11:59
I've run your sample, and it works with or without option explicit/option strict. It's correct.
– reckface
Mar 25 at 13:35
Thanks for confirming that it's valid code. I can only assume that the XtraReports application does not like Linq queries within its scripting editor.
– OWSam
Mar 25 at 14:28
add a comment |
TurnOption Strict On
...
– Çöđěxěŕ
Mar 25 at 11:59
I've run your sample, and it works with or without option explicit/option strict. It's correct.
– reckface
Mar 25 at 13:35
Thanks for confirming that it's valid code. I can only assume that the XtraReports application does not like Linq queries within its scripting editor.
– OWSam
Mar 25 at 14:28
Turn
Option Strict On
...– Çöđěxěŕ
Mar 25 at 11:59
Turn
Option Strict On
...– Çöđěxěŕ
Mar 25 at 11:59
I've run your sample, and it works with or without option explicit/option strict. It's correct.
– reckface
Mar 25 at 13:35
I've run your sample, and it works with or without option explicit/option strict. It's correct.
– reckface
Mar 25 at 13:35
Thanks for confirming that it's valid code. I can only assume that the XtraReports application does not like Linq queries within its scripting editor.
– OWSam
Mar 25 at 14:28
Thanks for confirming that it's valid code. I can only assume that the XtraReports application does not like Linq queries within its scripting editor.
– OWSam
Mar 25 at 14:28
add a comment |
1 Answer
1
active
oldest
votes
You could use GroupBy
method:
Sub Main()
Dim dt As New DataTable()
dt.Columns.Add("Category")
dt.Columns.Add("Net")
dt.Rows.Add("Hat", 1)
dt.Rows.Add("Hat", 2)
dt.Rows.Add("Gloves", 3)
dt.Rows.Add("Gloves", 4)
Dim dtSum = dt.AsEnumerable().GroupBy(
Function(row)
Return row.Field(Of String)("Category").ToString()
End Function).ToDictionary(Function(grp)
Return New KeyValuePair(Of String, Integer)(grp.Key, grp.Sum(Function(row)
Return Integer.Parse(row("Net").ToString())
End Function))
End Function)
End Sub
This should give an idea how to achieve what you want.
You needDim dt As New DataTable() dt.Columns.Add("Category") dt.Columns.Add("DataSort", GetType(Integer)) dt.Columns.Add("Net", GetType(Decimal))
for this to match the question
– reckface
Mar 25 at 13:37
@reckface It's totally irrelevant to the answer. It shows what method to use and how to achieve desired result
– Michał Turczyn
Mar 25 at 13:44
fair enough. The op wants an output table that matches the input table but with the sum of the net column. His error shouldn't even occur, this may not address that.
– reckface
Mar 25 at 14:04
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%2f55336949%2flinq-group-by-and-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
You could use GroupBy
method:
Sub Main()
Dim dt As New DataTable()
dt.Columns.Add("Category")
dt.Columns.Add("Net")
dt.Rows.Add("Hat", 1)
dt.Rows.Add("Hat", 2)
dt.Rows.Add("Gloves", 3)
dt.Rows.Add("Gloves", 4)
Dim dtSum = dt.AsEnumerable().GroupBy(
Function(row)
Return row.Field(Of String)("Category").ToString()
End Function).ToDictionary(Function(grp)
Return New KeyValuePair(Of String, Integer)(grp.Key, grp.Sum(Function(row)
Return Integer.Parse(row("Net").ToString())
End Function))
End Function)
End Sub
This should give an idea how to achieve what you want.
You needDim dt As New DataTable() dt.Columns.Add("Category") dt.Columns.Add("DataSort", GetType(Integer)) dt.Columns.Add("Net", GetType(Decimal))
for this to match the question
– reckface
Mar 25 at 13:37
@reckface It's totally irrelevant to the answer. It shows what method to use and how to achieve desired result
– Michał Turczyn
Mar 25 at 13:44
fair enough. The op wants an output table that matches the input table but with the sum of the net column. His error shouldn't even occur, this may not address that.
– reckface
Mar 25 at 14:04
add a comment |
You could use GroupBy
method:
Sub Main()
Dim dt As New DataTable()
dt.Columns.Add("Category")
dt.Columns.Add("Net")
dt.Rows.Add("Hat", 1)
dt.Rows.Add("Hat", 2)
dt.Rows.Add("Gloves", 3)
dt.Rows.Add("Gloves", 4)
Dim dtSum = dt.AsEnumerable().GroupBy(
Function(row)
Return row.Field(Of String)("Category").ToString()
End Function).ToDictionary(Function(grp)
Return New KeyValuePair(Of String, Integer)(grp.Key, grp.Sum(Function(row)
Return Integer.Parse(row("Net").ToString())
End Function))
End Function)
End Sub
This should give an idea how to achieve what you want.
You needDim dt As New DataTable() dt.Columns.Add("Category") dt.Columns.Add("DataSort", GetType(Integer)) dt.Columns.Add("Net", GetType(Decimal))
for this to match the question
– reckface
Mar 25 at 13:37
@reckface It's totally irrelevant to the answer. It shows what method to use and how to achieve desired result
– Michał Turczyn
Mar 25 at 13:44
fair enough. The op wants an output table that matches the input table but with the sum of the net column. His error shouldn't even occur, this may not address that.
– reckface
Mar 25 at 14:04
add a comment |
You could use GroupBy
method:
Sub Main()
Dim dt As New DataTable()
dt.Columns.Add("Category")
dt.Columns.Add("Net")
dt.Rows.Add("Hat", 1)
dt.Rows.Add("Hat", 2)
dt.Rows.Add("Gloves", 3)
dt.Rows.Add("Gloves", 4)
Dim dtSum = dt.AsEnumerable().GroupBy(
Function(row)
Return row.Field(Of String)("Category").ToString()
End Function).ToDictionary(Function(grp)
Return New KeyValuePair(Of String, Integer)(grp.Key, grp.Sum(Function(row)
Return Integer.Parse(row("Net").ToString())
End Function))
End Function)
End Sub
This should give an idea how to achieve what you want.
You could use GroupBy
method:
Sub Main()
Dim dt As New DataTable()
dt.Columns.Add("Category")
dt.Columns.Add("Net")
dt.Rows.Add("Hat", 1)
dt.Rows.Add("Hat", 2)
dt.Rows.Add("Gloves", 3)
dt.Rows.Add("Gloves", 4)
Dim dtSum = dt.AsEnumerable().GroupBy(
Function(row)
Return row.Field(Of String)("Category").ToString()
End Function).ToDictionary(Function(grp)
Return New KeyValuePair(Of String, Integer)(grp.Key, grp.Sum(Function(row)
Return Integer.Parse(row("Net").ToString())
End Function))
End Function)
End Sub
This should give an idea how to achieve what you want.
answered Mar 25 at 13:24
Michał TurczynMichał Turczyn
18.4k13 gold badges22 silver badges41 bronze badges
18.4k13 gold badges22 silver badges41 bronze badges
You needDim dt As New DataTable() dt.Columns.Add("Category") dt.Columns.Add("DataSort", GetType(Integer)) dt.Columns.Add("Net", GetType(Decimal))
for this to match the question
– reckface
Mar 25 at 13:37
@reckface It's totally irrelevant to the answer. It shows what method to use and how to achieve desired result
– Michał Turczyn
Mar 25 at 13:44
fair enough. The op wants an output table that matches the input table but with the sum of the net column. His error shouldn't even occur, this may not address that.
– reckface
Mar 25 at 14:04
add a comment |
You needDim dt As New DataTable() dt.Columns.Add("Category") dt.Columns.Add("DataSort", GetType(Integer)) dt.Columns.Add("Net", GetType(Decimal))
for this to match the question
– reckface
Mar 25 at 13:37
@reckface It's totally irrelevant to the answer. It shows what method to use and how to achieve desired result
– Michał Turczyn
Mar 25 at 13:44
fair enough. The op wants an output table that matches the input table but with the sum of the net column. His error shouldn't even occur, this may not address that.
– reckface
Mar 25 at 14:04
You need
Dim dt As New DataTable() dt.Columns.Add("Category") dt.Columns.Add("DataSort", GetType(Integer)) dt.Columns.Add("Net", GetType(Decimal))
for this to match the question– reckface
Mar 25 at 13:37
You need
Dim dt As New DataTable() dt.Columns.Add("Category") dt.Columns.Add("DataSort", GetType(Integer)) dt.Columns.Add("Net", GetType(Decimal))
for this to match the question– reckface
Mar 25 at 13:37
@reckface It's totally irrelevant to the answer. It shows what method to use and how to achieve desired result
– Michał Turczyn
Mar 25 at 13:44
@reckface It's totally irrelevant to the answer. It shows what method to use and how to achieve desired result
– Michał Turczyn
Mar 25 at 13:44
fair enough. The op wants an output table that matches the input table but with the sum of the net column. His error shouldn't even occur, this may not address that.
– reckface
Mar 25 at 14:04
fair enough. The op wants an output table that matches the input table but with the sum of the net column. His error shouldn't even occur, this may not address that.
– reckface
Mar 25 at 14:04
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%2f55336949%2flinq-group-by-and-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
Turn
Option Strict On
...– Çöđěxěŕ
Mar 25 at 11:59
I've run your sample, and it works with or without option explicit/option strict. It's correct.
– reckface
Mar 25 at 13:35
Thanks for confirming that it's valid code. I can only assume that the XtraReports application does not like Linq queries within its scripting editor.
– OWSam
Mar 25 at 14:28