Hi all,
I am trying multiple ways to compare 4 different cols within a single database but none has worked.
Can you please provide a linq to compare 4 cols within same database? Need to check if they have the same exact amount.
Thank you
Hi all,
I am trying multiple ways to compare 4 different cols within a single database but none has worked.
Can you please provide a linq to compare 4 cols within same database? Need to check if they have the same exact amount.
Thank you
Hi @m.soto,
From row In dt.AsEnumerable()
Where Not row.IsNull("col1") AndAlso
Not row.IsNull("col2") AndAlso
Not row.IsNull("col3") AndAlso
Not row.IsNull("col4") AndAlso
row.Field(Of Integer)("col1") = row.Field(Of Integer)("col2") AndAlso
row.Field(Of Integer)("col2") = row.Field(Of Integer)("col3") AndAlso
row.Field(Of Integer)("col3") = row.Field(Of Integer)("col4")
Select row
If this query does not return an empty DataTable, then you can say that your original DataTable had the same amounts in those 4 columns.
Cheers,
Kardelen
Please try this…it will return true if all values match else false even if one row fails
Dt.AsEnumerable.All(function(x) x("Col1").ToString.Equals(x("Col2").ToString) AndAlso x("Col1").ToString.Equals(x("Col3").ToString) AndAlso x("Col1").ToString.Equals(x("Col4").ToString))
Cheers
Hey @m.soto,
To compare 4 different columns within a single DataTable using LINQ, you can use the following approach. This LINQ query checks if the values in the 4 columns are identical across all rows:
Dim result = (From row In dt.AsEnumerable()
Where row("Col1").ToString() = row("Col2").ToString() AndAlso
row("Col2").ToString() = row("Col3").ToString() AndAlso
row("Col3").ToString() = row("Col4").ToString()
Select row).CopyToDataTable()
Col1, Col2, Col3, Col4).CopyToDataTable() in a Try-Catch block if there’s a chance of no matching rows to avoid exceptions.Please mark this as the solution if it helps! ![]()
Best,
Chaitanya