Getting all elements while using group by on string column using LINQ
I want to group by datatable using followng method. However i can not select a column which includes string.
dtTAConvert example;
SAMPLE_TIME WAIT_CLASS
23:11:12 CPU
23:11:12 IO
23:11:12 IO
23:11:11 CPU
23:11:11 CPU
What i want is this;
SAMPLE_TIME WAIT_CLASS COUNT
23:11:12 CPU 1
23:11:12 IO 2
23:11:11 CPU 2
So far, i wrote below code;
var dtTAConvertList =
(from dr1 in dtTAConvert.AsEnumerable()
group dr1 by dr1.Field<DateTime>("SAMPLE_TIME") into g
select new
{
SAMPLE_TIME = g.Key,
WAIT_CLASS = g.Field<string>("WAIT_CLASS"), // ==> I get error in this line
COUNT = Math.Round(g.Sum(h => h.Field<double>("COUNT")) / 15.0, 2),
});
It throws the exception
'IGrouping' does not contain a definition for 'Field' and the best extension method overload 'DataRowExtensions.Field(DataRow, string)' requires a receiver of type 'DataRow'
If i change above code to like below, it works;
WAIT_CLASS = g.First().Field<string>("WAIT_CLASS"),
However this returns only the first element, which is not work for me.
How can i select string column like as i explained?
c# .net linq datatable datarow
add a comment |
I want to group by datatable using followng method. However i can not select a column which includes string.
dtTAConvert example;
SAMPLE_TIME WAIT_CLASS
23:11:12 CPU
23:11:12 IO
23:11:12 IO
23:11:11 CPU
23:11:11 CPU
What i want is this;
SAMPLE_TIME WAIT_CLASS COUNT
23:11:12 CPU 1
23:11:12 IO 2
23:11:11 CPU 2
So far, i wrote below code;
var dtTAConvertList =
(from dr1 in dtTAConvert.AsEnumerable()
group dr1 by dr1.Field<DateTime>("SAMPLE_TIME") into g
select new
{
SAMPLE_TIME = g.Key,
WAIT_CLASS = g.Field<string>("WAIT_CLASS"), // ==> I get error in this line
COUNT = Math.Round(g.Sum(h => h.Field<double>("COUNT")) / 15.0, 2),
});
It throws the exception
'IGrouping' does not contain a definition for 'Field' and the best extension method overload 'DataRowExtensions.Field(DataRow, string)' requires a receiver of type 'DataRow'
If i change above code to like below, it works;
WAIT_CLASS = g.First().Field<string>("WAIT_CLASS"),
However this returns only the first element, which is not work for me.
How can i select string column like as i explained?
c# .net linq datatable datarow
in your expected output it seems you want to group by bothSAMPLE_TIMEandWAIT_CLASSbut you're not doing this in your query. any reasoning?
– Aomine
Dec 27 '18 at 19:54
@Aomine well, i was not know that. I am really bad at linQ. How can i group both ?
– doctor cesar
Dec 27 '18 at 19:56
add a comment |
I want to group by datatable using followng method. However i can not select a column which includes string.
dtTAConvert example;
SAMPLE_TIME WAIT_CLASS
23:11:12 CPU
23:11:12 IO
23:11:12 IO
23:11:11 CPU
23:11:11 CPU
What i want is this;
SAMPLE_TIME WAIT_CLASS COUNT
23:11:12 CPU 1
23:11:12 IO 2
23:11:11 CPU 2
So far, i wrote below code;
var dtTAConvertList =
(from dr1 in dtTAConvert.AsEnumerable()
group dr1 by dr1.Field<DateTime>("SAMPLE_TIME") into g
select new
{
SAMPLE_TIME = g.Key,
WAIT_CLASS = g.Field<string>("WAIT_CLASS"), // ==> I get error in this line
COUNT = Math.Round(g.Sum(h => h.Field<double>("COUNT")) / 15.0, 2),
});
It throws the exception
'IGrouping' does not contain a definition for 'Field' and the best extension method overload 'DataRowExtensions.Field(DataRow, string)' requires a receiver of type 'DataRow'
If i change above code to like below, it works;
WAIT_CLASS = g.First().Field<string>("WAIT_CLASS"),
However this returns only the first element, which is not work for me.
How can i select string column like as i explained?
c# .net linq datatable datarow
I want to group by datatable using followng method. However i can not select a column which includes string.
dtTAConvert example;
SAMPLE_TIME WAIT_CLASS
23:11:12 CPU
23:11:12 IO
23:11:12 IO
23:11:11 CPU
23:11:11 CPU
What i want is this;
SAMPLE_TIME WAIT_CLASS COUNT
23:11:12 CPU 1
23:11:12 IO 2
23:11:11 CPU 2
So far, i wrote below code;
var dtTAConvertList =
(from dr1 in dtTAConvert.AsEnumerable()
group dr1 by dr1.Field<DateTime>("SAMPLE_TIME") into g
select new
{
SAMPLE_TIME = g.Key,
WAIT_CLASS = g.Field<string>("WAIT_CLASS"), // ==> I get error in this line
COUNT = Math.Round(g.Sum(h => h.Field<double>("COUNT")) / 15.0, 2),
});
It throws the exception
'IGrouping' does not contain a definition for 'Field' and the best extension method overload 'DataRowExtensions.Field(DataRow, string)' requires a receiver of type 'DataRow'
If i change above code to like below, it works;
WAIT_CLASS = g.First().Field<string>("WAIT_CLASS"),
However this returns only the first element, which is not work for me.
How can i select string column like as i explained?
c# .net linq datatable datarow
c# .net linq datatable datarow
edited Dec 27 '18 at 20:11
Aomine
40.2k73870
40.2k73870
asked Dec 27 '18 at 19:51
doctor cesar
82
82
in your expected output it seems you want to group by bothSAMPLE_TIMEandWAIT_CLASSbut you're not doing this in your query. any reasoning?
– Aomine
Dec 27 '18 at 19:54
@Aomine well, i was not know that. I am really bad at linQ. How can i group both ?
– doctor cesar
Dec 27 '18 at 19:56
add a comment |
in your expected output it seems you want to group by bothSAMPLE_TIMEandWAIT_CLASSbut you're not doing this in your query. any reasoning?
– Aomine
Dec 27 '18 at 19:54
@Aomine well, i was not know that. I am really bad at linQ. How can i group both ?
– doctor cesar
Dec 27 '18 at 19:56
in your expected output it seems you want to group by both
SAMPLE_TIME and WAIT_CLASS but you're not doing this in your query. any reasoning?– Aomine
Dec 27 '18 at 19:54
in your expected output it seems you want to group by both
SAMPLE_TIME and WAIT_CLASS but you're not doing this in your query. any reasoning?– Aomine
Dec 27 '18 at 19:54
@Aomine well, i was not know that. I am really bad at linQ. How can i group both ?
– doctor cesar
Dec 27 '18 at 19:56
@Aomine well, i was not know that. I am really bad at linQ. How can i group both ?
– doctor cesar
Dec 27 '18 at 19:56
add a comment |
1 Answer
1
active
oldest
votes
You're receiving the said error because as shown IGrouping i.e. a collection of objects that have a common key does not contain a definition for 'Field'. you probably expected this to be a DataRow.
In addition, currently you're grouping only by SAMPLE_TIME which is not what you need, rather you need to group by both SAMPLE_TIME and WAIT_CLASS to get the aforementioned result.
Here's how to group by both of those fields via query syntax:
var result = from dr in dtTAConvert.AsEnumerable()
group dr by new
{
sampleTime = dr.Field<DateTime>("SAMPLE_TIME"),
waitClass = dr.Field<string>("WAIT_CLASS")
} into g
select new
{
SampleTime = g.Key.sampleTime,
WaitClass = g.Key.waitClass,
Count = g.Count()
});
or fluent syntax:
var result = dtTAConvert.AsEnumerable()
.GroupBy(dr => new
{
sampleTime = dr.Field<DateTime>("SAMPLE_TIME"),
waitClass = dr.Field<string>("WAIT_CLASS")
}).Select(g => new
{
SampleTime = g.Key.sampleTime,
WaitClass = g.Key.waitClass,
Count = g.Count()
});
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%2f53950182%2fgetting-all-elements-while-using-group-by-on-string-column-using-linq%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're receiving the said error because as shown IGrouping i.e. a collection of objects that have a common key does not contain a definition for 'Field'. you probably expected this to be a DataRow.
In addition, currently you're grouping only by SAMPLE_TIME which is not what you need, rather you need to group by both SAMPLE_TIME and WAIT_CLASS to get the aforementioned result.
Here's how to group by both of those fields via query syntax:
var result = from dr in dtTAConvert.AsEnumerable()
group dr by new
{
sampleTime = dr.Field<DateTime>("SAMPLE_TIME"),
waitClass = dr.Field<string>("WAIT_CLASS")
} into g
select new
{
SampleTime = g.Key.sampleTime,
WaitClass = g.Key.waitClass,
Count = g.Count()
});
or fluent syntax:
var result = dtTAConvert.AsEnumerable()
.GroupBy(dr => new
{
sampleTime = dr.Field<DateTime>("SAMPLE_TIME"),
waitClass = dr.Field<string>("WAIT_CLASS")
}).Select(g => new
{
SampleTime = g.Key.sampleTime,
WaitClass = g.Key.waitClass,
Count = g.Count()
});
add a comment |
You're receiving the said error because as shown IGrouping i.e. a collection of objects that have a common key does not contain a definition for 'Field'. you probably expected this to be a DataRow.
In addition, currently you're grouping only by SAMPLE_TIME which is not what you need, rather you need to group by both SAMPLE_TIME and WAIT_CLASS to get the aforementioned result.
Here's how to group by both of those fields via query syntax:
var result = from dr in dtTAConvert.AsEnumerable()
group dr by new
{
sampleTime = dr.Field<DateTime>("SAMPLE_TIME"),
waitClass = dr.Field<string>("WAIT_CLASS")
} into g
select new
{
SampleTime = g.Key.sampleTime,
WaitClass = g.Key.waitClass,
Count = g.Count()
});
or fluent syntax:
var result = dtTAConvert.AsEnumerable()
.GroupBy(dr => new
{
sampleTime = dr.Field<DateTime>("SAMPLE_TIME"),
waitClass = dr.Field<string>("WAIT_CLASS")
}).Select(g => new
{
SampleTime = g.Key.sampleTime,
WaitClass = g.Key.waitClass,
Count = g.Count()
});
add a comment |
You're receiving the said error because as shown IGrouping i.e. a collection of objects that have a common key does not contain a definition for 'Field'. you probably expected this to be a DataRow.
In addition, currently you're grouping only by SAMPLE_TIME which is not what you need, rather you need to group by both SAMPLE_TIME and WAIT_CLASS to get the aforementioned result.
Here's how to group by both of those fields via query syntax:
var result = from dr in dtTAConvert.AsEnumerable()
group dr by new
{
sampleTime = dr.Field<DateTime>("SAMPLE_TIME"),
waitClass = dr.Field<string>("WAIT_CLASS")
} into g
select new
{
SampleTime = g.Key.sampleTime,
WaitClass = g.Key.waitClass,
Count = g.Count()
});
or fluent syntax:
var result = dtTAConvert.AsEnumerable()
.GroupBy(dr => new
{
sampleTime = dr.Field<DateTime>("SAMPLE_TIME"),
waitClass = dr.Field<string>("WAIT_CLASS")
}).Select(g => new
{
SampleTime = g.Key.sampleTime,
WaitClass = g.Key.waitClass,
Count = g.Count()
});
You're receiving the said error because as shown IGrouping i.e. a collection of objects that have a common key does not contain a definition for 'Field'. you probably expected this to be a DataRow.
In addition, currently you're grouping only by SAMPLE_TIME which is not what you need, rather you need to group by both SAMPLE_TIME and WAIT_CLASS to get the aforementioned result.
Here's how to group by both of those fields via query syntax:
var result = from dr in dtTAConvert.AsEnumerable()
group dr by new
{
sampleTime = dr.Field<DateTime>("SAMPLE_TIME"),
waitClass = dr.Field<string>("WAIT_CLASS")
} into g
select new
{
SampleTime = g.Key.sampleTime,
WaitClass = g.Key.waitClass,
Count = g.Count()
});
or fluent syntax:
var result = dtTAConvert.AsEnumerable()
.GroupBy(dr => new
{
sampleTime = dr.Field<DateTime>("SAMPLE_TIME"),
waitClass = dr.Field<string>("WAIT_CLASS")
}).Select(g => new
{
SampleTime = g.Key.sampleTime,
WaitClass = g.Key.waitClass,
Count = g.Count()
});
answered Dec 27 '18 at 20:05
Aomine
40.2k73870
40.2k73870
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53950182%2fgetting-all-elements-while-using-group-by-on-string-column-using-linq%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
in your expected output it seems you want to group by both
SAMPLE_TIMEandWAIT_CLASSbut you're not doing this in your query. any reasoning?– Aomine
Dec 27 '18 at 19:54
@Aomine well, i was not know that. I am really bad at linQ. How can i group both ?
– doctor cesar
Dec 27 '18 at 19:56