1

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.

2 Answers 2

4

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")) }); 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.