Find Distinct elements and its corresponding row from Data Table


I have Col1,Col2,Col3,Col4

Where i have to get distinct elements from col1 and its corresponding Col3 value and store the data in dictionary format(Key : Col1 , Value :
Col3).Can someone please help?

Hi @JITU99,
Refer below link



Can you try the following expression?

dict = dt.AsEnumerable.GroupBy(Function(r) r("Col1").ToString).Where(Function(g) g.Count=1).ToDictionary(Function(g) g.Key,Function(g) g.First().Item("Col3").ToString ) (2.8 KB)


@Yoichi Your solution was helpful fr abit but actually i want all the unique elements like take the first element and ignore the next duplicates.Can you please help with it?


Alright. How about the following?

dict = dt.AsEnumerable.GroupBy(Function(r) r("Col1").ToString).ToDictionary(Function(g) g.Key,Function(g) g.First().Item("Col3").ToString ) (2.8 KB)


It helped thank you.

1 Like

thanks a lot :slight_smile: @Yoichi @arivu96 both the solutions really helped infact yoichi solution was more of efficient for my scenario.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.