简体   繁体   中英

Using LINQ get the differences between two tables

I have an existing table and a new table. The tables contain at least two items that need to be checked. I need to do the following items

1) Get a list of items that are not in the new table but are in the existing table. - So they can be removed 2) Get a list of items that are in the new table but not in the existing table - So they can be added

3) Get a list of items that are in both tables but the existing table needs updateing

Here is the data

var existingItems = new[] 
{ 
    new RetryItem { CellId = 1, Content = "Bob" }, 
    new RetryItem { CellId = 2, Content = "Bill" }, 
    new RetryItem { CellId = 3, Content = "Frank" }, 
    new RetryItem { CellId = 4, Content = "Tom" }, 
    new RetryItem { CellId = 5, Content = "Dick" }, 
    new RetryItem { CellId = 6, Content = "Harry" }, 
}; 

var newItemsLarger = new[] 
{ 
    new RetryItem { CellId = 1, Content = "Bob" }, 
    new RetryItem { CellId = 2, Content = "Bill" }, 
    new RetryItem { CellId = 3, Content = "Frank" }, 
    new RetryItem { CellId = 4, Content = "Tom now Thoams" }, 
    new RetryItem { CellId = 5, Content = "Dick now Dicky" }, 
    new RetryItem { CellId = 6, Content = "Harry Now Harriet" }, 
    new RetryItem { CellId = 7, Content = "Mary" }, 
    new RetryItem { CellId = 8, Content = "Mungo" }, 
    new RetryItem { CellId = 9, Content = "Midge" }, 
};

I think those might meet your needs:

1)

var q1 = from c1 in existingItems
         join c2 in newItemsLarger 
         on new { c1.CellId, c1.Content } equals new {c2.CellId, c2.Content }
         select c1;

2)

var q2 = from c1 in newItemsLarger
         where !existingItems.Select(x => x.CellId).Contains(c1.CellId)
         select c1;

3)

var q3 = from c1 in existingItems
         join c2 in newItemsLarger on c1.CellId equals c2.CellId 
         where c1.Content != c2.Content
         select c2;

You can use the following sample program in a console application:

using System.Linq;

namespace ExperimentConsoleApp
{
    class Program
    {
        static void Main()
        {
            // Check if the item is in existingItems but not in newItems
            var itemsToBeRemoved = (from e in existingItems
                                    where !newItemsLarger.Any(n => n.CellId == e.CellId)
                                    select e).ToList();

            // Check if the item is in newItems but not in existingItems 
            var itemsToBeAdded = (from n in newItemsLarger
                                  where !existingItems.Any(e => n.CellId == e.CellId)
                                  select n).ToList();

            // Match the items on Id and check if their contents equals
            var itemsToBeUpdated = (from e in existingItems
                                    from n in newItemsLarger
                                    where e.CellId == n.CellId && e.Content != n.Content
                                    select n).ToList();
        }

        static RetryItem[] existingItems = new[] 
                        { 
                            new RetryItem { CellId = 1, Content = "Bob" }, 
                            new RetryItem { CellId = 2, Content = "Bill" }, 
                            new RetryItem { CellId = 3, Content = "Frank" }, 
                            new RetryItem { CellId = 4, Content = "Tom" }, 
                            new RetryItem { CellId = 5, Content = "Dick" }, 
                            new RetryItem { CellId = 6, Content = "Harry" }, 
                        };

        static RetryItem[] newItemsLarger = new[] 
                        { 
                            new RetryItem { CellId = 1, Content = "Bob" }, 
                            new RetryItem { CellId = 3, Content = "Frank" }, 
                            new RetryItem { CellId = 4, Content = "Tom now Thoams" }, 
                            new RetryItem { CellId = 5, Content = "Dick now Dicky" }, 
                            new RetryItem { CellId = 6, Content = "Harry Now Harriet" }, 
                            new RetryItem { CellId = 7, Content = "Mary" }, 
                            new RetryItem { CellId = 8, Content = "Mungo" }, 
                            new RetryItem { CellId = 9, Content = "Midge" }, 
                        };
    }

    public class RetryItem
    {
        public int CellId { get; set; }
        public string Content { get; set; }
    }
}

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