How to check if the data in a column of a data table has the same data?

The result that I want to achieve is this:

Line ItemNumber DeliveryDate
1 100 02.06.2019
2 200 02.06.2019
3 300 02.06.2019
4 400 02.06.2019
5 500 02.06.2019
6 600 02.06.2019

If the delivery dates are all the same then I want to create a Boolean and say SameDelivery = True

Line ItemNumber DeliveryDate
1 100 02.06.2019
2 200 02.06.2019
3 300 02.06.2019
4 400 02.07.2019
5 500 02.08.2019
6 600 02.09.2019

If the delivery dates are not all the same then I want to create a Boolean and say SameDelivery = False

How do I check if the DeliveryDate are all the same in the DeliveryDate column?

@skyblue1854
In general it can be done with counting the Distinct Values from Delivery Date

Assign Activity
To: CountResultVar As Int32
Value: YourDataTableVar.AsEnumerable.Select(Function (row) row(‚ÄúDelivery Date‚ÄĚ).toString).Distinct().Count

Then with an If else you can decide if its true or false

Would this work for you?

Technically doing it in a one liner could look like:
YourDataTableVar.AsEnumerable.Select(Function (row) row(‚ÄúDelivery Date‚ÄĚ).toString).Distinct().ToList.TrueForAll(Function (d) d.Equals(dtSample(0)(‚ÄúDelivery Date‚ÄĚ).toString))

and should return true or false

1 Like

Thanks for the input! I am still having trouble with it. Would you please provide a working sample project file? Thanks again.

Nevermind. I figured it out.

It works as expected now. I am using the one liner that you provided. I just needed to assign YourDataTableVar as Boolean. Thanks so much! You are amazing.

@ppr Now that everything works as expected, do you mind explaining this code so I can understand why it works?

YourDataTableVar.AsEnumerable.Select(Function (row) row(‚ÄúDelivery Date‚ÄĚ).toString).Distinct().ToList.TrueForAll(Function (d) d.Equals(dtSample(0)(‚ÄúDelivery Date‚ÄĚ).toString))

@skyblue1854
Statement is about DataSetExtensions/LINQ

YourDataTableVar.AsEnumerable | makes the datasetextensions available
Select(Function (row) row(‚ÄúDelivery Date‚ÄĚ).toString) | projecting for each row the column(‚ÄúDelivery Date‚ÄĚ) Value as String to further statement to the chain
Distinct() | eleminates the duplicated values
ToList | converts the remaining strings to a list
TrueForAll(Function (d) d.Equals(dtSample(0)(‚ÄúDelivery Date‚ÄĚ).toString)) | checks for all remaining strings if the value is the same and compairs with datatable first row column(‚ÄúDelivery Date‚ÄĚ)

A nice information ressource to LINQ is here:
https://www.tutorialsteacher.com/linq/linq-tutorials

I am happy that it is working at your end. Feel free to flag the helping post as solution, so others can benefit from it

Thank you so much for your time and help on this! I learnt a lot.

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