2012-01-06 42 views
2

我有一个现有的表和一个新的表。 这些表格至少包含两个需要检查的项目。 我需要做以下事项使用LINQ获取两个表之间的差异

1)获取不在新表中但在现有表中的项目列表。 - 所以他们可以被删除 2)获取在新表中但不在现有表中的项目列表 - 所以他们可以被添加

3)获取两个表中的项目列表,但现有的表需要updateing

下面是数据

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" }, 
}; 
+1

看一看[相交](HTTP:// MSDN .microsoft.com/en-us/library/bb460136.aspx)和[Except](http://msdn.microsoft.com/zh-cn/library/bb300779.aspx)。 – 2012-01-06 10:03:16

回答

1

您可以在控制台应用程序中使用下面的示例程序:

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; } 
    } 
} 
+0

我不知道LINQ对象实际上会为这些做什么...可能是'O(n^2)'? – 2012-01-06 10:07:38

+0

@pst我不得不说我现在不会这样..我用这样的东西(只在删除/删除)与linq实体和​​该性能查询是可以接受的(这仍然是主观) – 2012-01-06 10:13:02

+0

刚刚尝试它看起来非常好,很好理解。当更新的列表包含更少的项目时也起作用。 – Kaya 2012-01-06 10:50:15

3

我瘦ķ那些可能会满足您的需求:

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; 
相关问题