我有一个表格,其中包含应该通过电子邮件发送的用户ID。当他们通过电子邮件发送时,会将记录添加到另一个表中,并通知他们。我需要编写一个linq查询,它返回第一个表中不在通知表中的任何记录。有人知道怎么做吗?感谢。
答案 0 :(得分:1)
在伪代码中,这是你应该做的(左连接):
List<int?> listA = new List<int?>() { 1, 2, 3, 4, 5, 6, 7 };
List<int?> listB = new List<int?>() { 1, 2, 3, 4, 5 };
var result = (from a in listA
join b in listB on a equals b into subset
from c in subset.DefaultIfEmpty()
where !c.HasValue
select a).ToList();
这将生成一个结果值列表,分别为6和7。
您应该在这里查看值,就好像它们是表格中的PK和FK一样。
答案 1 :(得分:0)
第二个表的结果为空的外连接怎么样?
这是来自http://www.hookedonlinq.com/OuterJoinSample.ashx,稍作修改只包括那些没有匹配记录的记录:
string[] words = {"walking","walked","bouncing","bounced","bounce","talked","running"};
string[] suffixes = {"ing","ed","er","iest"};
var pairs = from word in words
from suffix in (from suffix in suffixes where word.EndsWith(suffix) select suffix).DefaultIfEmpty()
where String.IsNullOrEmpty(suffix)
select new {word, suffix};
Console.WriteLine(pairs);