How to consider only certain columns while data table filtering?

I’m using a filtering expression like

DT1.AsEnumerable.Except(DT2.AsEnumerable, System.Data.DataRowComparer.Default).CopyToDataTable() and it is working well but, I want it to perform the operation based on two cols only like based on Emp No and Emp Name only this should be done it shouldn’t consider other columns…

can this be achieved? Plz help

@Lahiru.Fernando sir can u plz take a look??

Hello @cybzom
You could try something like this

DT1.AsEnumerable().Where(function(row) DT.AsEnumerable().Select(function® r.Field(Of Int32)(“ID”)).Any(function(x) x = row.Field(Of Int32)(“ID”))).CopyToDataTable

do you want to compare two columns with each other or two columns seperately ?

1 Like

Why did u revoke it sir @vickydas??

@cybzom
With a Join Statement you can define the Common rows on the 2 column Base.

The result of IT can be used later for an except on dt1 or dt2

it’s not very clear sir can u elaborate it?

for a datatble with a lot of records performing a join operation and then filtering is going to take a lot of time… is there any other way??

I cant confirm this,

a Lot of records

is a vague, relative Volume description. And internal linq ist doing even iterations and filterings

From coding Viewpoint, you can write your custom datarow comparer

Another techniques is to build Key Arrays from the Data and doing similiar as your Statement from above

Or going the Filter any all method approach aa suggested above. But Here also internaly IT is done with loops and Filter as well

It’s not working… can u share workflow

Thnx… all I’m trying to do is I have 2 datatables…ok say A and B… I want to see what extra datarow B has that A doesn’t have so I’m using except but, that’s considering all the columns in the datatable to get the job done so i want to know if it can work only for certain cols…

any idea from ur side on how this can be done @ppr

@cybzom
Your starting right, with the except approach. The Rest to do ist still aß mentioned above

I’m sorry @vickydas this is not quite what I wanted

Let me explain what I want to do

I have two datatables say A and B with 4 cols [No, Name, Location] and keep in mind that the location will keep varying

Now, using an except expression like DT2.AsEnumerable.Except(DT1.AsEnumerable, System.Data.DataRowComparer.Default).CopyToDataTable is a problem since it also considers the location field so, it’s giving the odd one out based on all the fields

Can this be done? Kindly help me with this assignment @loginerror @Palaniyappan @vickydas

1 Like

@cybzom
I am writing from Cellphone maybe some correction needs to be done from your side

Finding the Common rows from dt1 and dt2 done with Join, Expression Syntax

(From d1 in dt1.asenumerable
Join d2 in dt2.asenumerable
On d1(“No”).toString equals d2(“No”).toString and d1(“Name”).toString equals d2(“Name”).toString
Select d2).copytodatable

So Just concat the Other cols in the Same manner, result are all dt2 rows that are Common with dt1

The result you can use later for the except Statement.

More clear to you now?

1 Like