Select rows with same value in column - Data Table

Hello,

I have a data table like below. I need to find out the unique policy number for all users. in below example, A001 is common policy number for all the users in the table. Can someone help me how to write the query using LINQ or any other possible way?

Name Policy
Jill A001
Eve A001
Eve A007
Tej A005
Suj A001
Tej A001

Do you know the common value or do you also need to check for the common value?

Also is there a particular series in which the policies are numbered or are they random?

The common values is unknown and order is random.

I’m not sure if this is the most elegant solution but you could try this.

  1. Count the number of unique users.
  2. Get unique policy numbers
  3. Loop through each policy number and Select rows from the data table matching the policy number. Count the rows matched
  4. If the rows count matches the unique users count that is your common policy number

Test1.xaml (11.4 KB)

Let me know if that works for you.

I know we can achieve this through LINQ any help would be much appriciated

Thanks for your answer…I though same solution…

@arivu96 Can you please help me LINQ query to get desired result.

Hey,

you can get the unique policy numbers probably like this:
dt1.AsEnumerable.Select(Function(r) r("Policy").ToString.Trim).Distinct

Then, you can use that in a For each, to loop over each unique Policy:

For each policy In dt1.AsEnumerable.Select(Function(r) r("Policy").ToString.Trim).Distinct
    For each row In dt1.AsEnumerable.Where(Function(r) r("Policy").ToString.Trim=policy ).ToArray
        <perform actions on each name>

So essentially, you loop through each unique policy, then using .Where() filter it to the unique policy to loop through each name of the unique policy. I hope that makes sense.

Hopefully, that helps. Regards.

Thanks Clayton, I will try this option.

Hi @Udayrayalla,

Use select command & use to take the data from policy