2014-02-06 64 views
0

除去重复行我有两个表Table_Access1和Table_Access 2.Linq查询从两个表中

---------------------------------------- 
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查询,任何帮助,将不胜感激。

+1

向我们展示您的尝试。 – gleng

+0

使用这些解决方案http://stackoverflow.com/questions/2561407/trying-to-get-distinct-values-from-two-listint-objects –

回答

0

它工作时,我用下面的:

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); 

感谢所有为您的输入。

+0

这看起来不正确。如果LookupID相同但ID不同? – ThunderGr

+0

在我的情况下,Access DTO对象将永远不会有Entity_ID不同。用户绑定到实现(Entity_ID),并且不能有多个实现与同一个用户。 – Kdev

+0

我明白了。这个问题并不明显。你谈到了这两者的独特*组合*。 – ThunderGr

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

如果Entity_ID不同并且Entity_LookupID相同或者它们两者不同? – ThunderGr

+0

您可以进行交叉连接,如图所示 –

+0

我在编辑之前编写了评论。您的初始代码使Entity_ID上的连接等于。该代码涵盖了两者中任何一个相等时的情况,但不包括两者不同时的情况(这也导致了独特的组合)。 – ThunderGr

0

这不是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) 
0

使用任何不应该对你所需要的工作。尽管你的请求看起来像你想要table1的第三条记录。

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