Hi all I am having a result set as follows
ID Name Price
1 XYZ 10
2 ABC 10
1 ABC 20
I would like to perform group by so that it should give me
ID Name Price
1 XYZ 10
ABC 20
2 ABC 10
I tried with the solution form the links but I couldn't get DataTable linq query to group by a column can some one help me
You can do something as simple as this
context.Table.GroupBy(t => t.ID);
This will return something of this type IQueryable<IGrouping<int, YourClass>>
(assuming your ID is an int)
Each entry will have a Key (the ID in this case) and an Enumerator so you can go through all the records grouped
Unless you want to change the original objects, you're going to have to project to a new collection and remove the repeated ID values. Something like:
var query =
data.GroupBy(d => d.ID)
.OrderBy(g => g.Key)
.SelectMany(g => (new[] {new {
g.First().ID,
g.First().Name,
g.First().Price}})
.Concat(g.Skip(1).Select(i => new {
ID = (string)null,
i.Name,
i.Price})));
essentially:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.