This is my datatable
X Y P
Str1 C1 10
Str1 C1 5
Str1 C1 1
Str1 C1 2
Str2 C1 25
Str2 C1 4
Str1 C2 411
Str1 C2 2356
Str2 C2 12
Str2 C2 33
I am trying to get distinct rows based on X and Y columns and get max value of P column using linq to dataset
X Y P
Str1 C1 10
Str2 C1 25
Str1 C2 2356
Str2 C2 33
-
var query0 = (from row in tablo.AsEnumerable()
select new
{
X = row.Field<string>("X"),
Y = row.Field<string>("Y"),
}).Distinct();
How can i do? Thanks.
You need GroupBy
here:-
var result = tablo.AsEnumerable().
.GroupBy(row => new
{
X= row.Field<string>("X"),
Y = row.Field<string>("Y")
})
.Select(x => new
{
X = x.Key.X,
Y = x.Key.Y,
P = x.Max(z => z.Field<int>("P"))
});
You could try something like this:
var result = tablo.AsEnumerable()
.Select(row=> new
{
X = row.Field<string>("X"),
Y = row.Field<string>("Y"),
P = row.Filed<int>("P")
}).GroupBy(i=>new {X = i.X, Y = i.Y})
.Select(gr=>new{
X = gr.Key.X,
Y = gr.Key.Y
P = gr.Max(z => z.P)
});
Essentially, we first declare a sequence of an anoymous type objects, that have three properties, X
, Y
and P
. Then we group by this sequence based on X
and Y
. Last, we pick up the maximum price for each group.
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.