简体   繁体   中英

C# Linq Query for compare two tables

I want linq c# query to compare two tables and list unmatched record from table 1

List<MyClass> Unmatched = new List<MyClass>();

foreach (var row in Table_A)
{
if (Table_B.Count(x => x.ID == row.ID) == 0)
Unmatched.Add(row);
}

Something like that?

It will only check Unmached Table1 to Table2. It doesn't check Table2 to Table1.

We need more details.

EDIT

The last line of code compares the elements of two lists and with ! Contains keep only the unmatched elements of the first list and added to the new Unmathced list:

List<string> table_1 = new List<string>() { "panos", "mitsos", "alex", "niki" };

List<string> table_2 = new List<string>() { "panos", "mitsos", "alexandros", "maria" };

List<string> UnmatchedList= new List<string>();

UnmatchedList = table_1.Where(x => !table_2.Contains(x)).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM