2011-04-19 141 views
1

我有一個表應該通過電子郵件發送的人的用戶名。當他們通過電子郵件發送時,一條記錄會被添加到他們收到通知的另一張表格中。我需要編寫一個linq查詢,返回第一個表中不在notifed表中的任何記錄。有人知道怎麼做嗎?謝謝。從一個表中選擇不在另一個表中的行與linq

+0

我在這裏找到答案 - http://programminglinq.com/blogs/marcorusso/archive/2008/01/14/the-not-in-clause-in-linq-to-sql.aspx – 2011-04-19 14:03:05

回答

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); 
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一樣。

相關問題