LINQ - How to check and get unmatching row values from tw0 datatable


I have two data table and i want to return duplicate rows in second table if any duplicate found for same combination.

if does not match rows then it should return as below

the above example empno and sub1 from TAB1 should check in TAB2.

if both match then no need to worry.

if doesn't match the particular row (101 and A) then we should return rows from TAB2

below is my sample code.

but, it shows some error..

where is problem in my code...?

 foreach (DataRow row in dt1.Rows)
            EmpNo = row["EmpNo"].ToString();
            Sub1 = row["Sub1"].ToString();

            var duplicates = dt2.AsEnumerable()
            .GroupBy(dr => row["EmpNo"].ToString() && row["Sub1"].ToString())
            .Where(g => g.Count() > 1)
            .Select(g => g.First())


Open in new window

pls. check my code...?
Gani tptTech Lead cum developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
From the 2 tables you show what would you want to return?
Gani tptTech Lead cum developerAuthor Commented:
i want to compare empno from tab1 to tab2.

if any empno found in tab2 then return corresponding sub1.

if tab1 of sub1 = tab2 of sub1 is equal then doesn't matter.

if tab1 of sub1 = tab2 of sub1 is doesn't equal then return "Subject " + A1" (A1 doesn't match)....
NorieAnalyst Assistant Commented:
Sorry, I don't follow - do you want to return rows that match or rows that don't match?
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Gani tptTech Lead cum developerAuthor Commented:
return don't match rows...
Fernando SotoRetiredCommented:
Hi Ganesh;

The following code snippet should do what you are looking for.
var dups = (from t1 in TAB1.AsEnumerable()
            from t2 in TAB2.AsEnumerable()
            where t1.Field<int>("EmpNo") == t2.Field<int>("EmpNo") && t1.Field<string>("Sub1") != t2.Field<string>("Sub1")
            select t2).ToList();

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gani tptTech Lead cum developerAuthor Commented:
Yes. Ofcourse it's matching.
Gani tptTech Lead cum developerAuthor Commented:
Yes. it's working...
Fernando SotoRetiredCommented:
Great, have a good day.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.