【问题标题】:Linq query to remove duplicate rows from two tables用于从两个表中删除重复行的 Linq 查询
【发布时间】:2014-02-06 14:22:36
【问题描述】:

我有两个表 Table_Access1 和 Table_Access 2。

----------------------------------------
Access_ID | Entity_ID | Entity_LookupID
----------------------------------------
   1      |   4       |  1
----------------------------------------
   2      |   4       |  2
---------------------------------------
   3      |   4       |  10
----------------------------------------  
__________________________________________

----------------------------------------
Access_ID | Entity_ID | Entity_LookupID
----------------------------------------
  0      |   4       |  1
----------------------------------------
   0      |   4       |  2
---------------------------------------
   0     |   4       |  11
---------------------------------------- 
   0     |   4       |  13
---------------------------------------- 

______________________________________

结果表:

----------------------------------------
Access_ID | Entity_ID | Entity_LookupID
----------------------------------------
  0      |   4       |  11
----------------------------------------
   0      |   4       |  13
---------------------------------------

对于TBL_Access1Access_ID 是主键,TBL_Access2 是来自DTO 的对象,没有Access_ID。我想从Table_Access 2 插入不重复的行。 Entity_IDEntity_LookupID 的组合将是唯一的。结果表就是我要找的输出。

我正在为 linq 查询而苦苦挣扎,我们将不胜感激。

【问题讨论】:

标签: c# sql linq


【解决方案1】:
from t2 in Table_Access2
//join t1 in Table_Access1 on t2.Entity_ID = t1.Entity_ID
from t1 in Table_Access1
where (t2.Entity_ID == t1.Entity_ID && t2.Entity_LookupID != t1.Entity_LookupID)
      || (t2.Entity_LookupID == t1.Entity_LookupID && t2.Entity_ID != t1.Entity_LookupID)
      || (t2.Entity_LookupID != t1.Entity_LookupID && t2.Entity_ID != t1.Entity_LookupID)
      )
select t2

【讨论】:

  • 如果 Entity_ID 不同而 Entity_LookupID 相同或两者不同怎么办?
  • 我在您编辑之前写了评论。您的初始代码使 Entity_ID 上的连接相等。此代码涵盖了两者中的任何一个相等的情况,但不包括两者不同的部分(这也导致了唯一的组合)。
  • 可以添加更多的OR条件
【解决方案2】:

这不是 linq 但是,因为它们是数据库表,你可以

 SELECT * FROM TBL_Access1 LEFT JOIN TBL_Access2 
     ON ( TBL_Access1.Entity_ID <> TBL_Access2.Entity_ID 
          AND TBL_Access1.Entity_LookupID <> TBL_Access2.Entity_LookupID )  
     OR ( TBL_Access1.Entity_ID <> TBL_Access2.Entity_ID 
          AND TBL_Access1.Entity_LookupID = TBL_Access2.Entity_LookupID ) 
     OR ( TBL_Access1.Entity_ID = TBL_Access2.Entity_ID 
          AND TBL_Access1.Entity_LookupID <> TBL_Access2.Entity_LookupID )

【讨论】:

    【解决方案3】:

    使用 not any 应该可以满足您的需要。尽管您的请求似乎想要 table1 中的第三条记录。

    var query = TBL_Access2
                .Where(t2 => !TBL_Access1
                .Any(t1 => t1.Entity_ID == t2.Entity_ID && t1.Entity_LookupID == t1.Entity_LookupID));
    

    【讨论】:

      【解决方案4】:

      当我使用以下内容时它起作用了:

      var userAccessInsert = (from r in TBL_Access2
                                      where !(from uA in TBL_Access1
                                             where uA.Entity_LookupID == r.Entity_LookupID
                                             select uA.Entity_LookupID).Contains(r.Entity_LookupID)
                                             select r);
      

      感谢大家的意见。

      【讨论】:

      • 这看起来不正确。如果 LookupID 相同但 ID 不同怎么办?
      • 就我而言,Access DTO 对象的 Entity_ID 永远不会不同。一个用户与一个实现(Entity_ID)相关联,它不能有多个具有同一个用户的实现。
      • 我明白了。从你的问题来看,这并不明显。您谈到了两者的独特组合
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      相关资源
      最近更新 更多