code_junkie

How would you do a “not in” query with Linq?

Nov 11th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. var matches = from item1 in list1
  2. join item2 in list2 on item1.Email equals item2.Email
  3. select new { Email = list1.Email };
  4.  
  5. NorthwindDataContext dc = new NorthwindDataContext();
  6. dc.Log = Console.Out;
  7.  
  8. var query =
  9. from c in dc.Customers
  10. where !(from o in dc.Orders
  11. select o.CustomerID)
  12. .Contains(c.CustomerID)
  13. select c;
  14.  
  15. foreach (var c in query) Console.WriteLine( c );
  16.  
  17. var answer = list1.Except(list2);
  18.  
  19. from item1 in List1
  20. where !(list2.Any(item2 => item2.Email == item1.Email))
  21. select item1;
  22.  
  23. var itemIds = inMemoryList.Select(x => x.Id).ToArray();
  24. var otherObjects = context.ItemList.Where(x => !itemIds.Contains(x.Id));
  25.  
  26. var secondEmails = (from item in list2
  27. select new { Email = item.Email }
  28. ).ToList();
  29.  
  30. var matches = from item in list1
  31. where !secondEmails.Contains(item.Email)
  32. select new {Email = item.Email};
  33.  
  34. List<int> list1 = new List<int>();
  35. // fill data
  36. List<int> list2 = new List<int>();
  37. // fill data
  38.  
  39. var results = from i in list1
  40. where !list2.Contains(i)
  41. select i;
  42.  
  43. foreach (var result in results)
  44. Console.WriteLine(result.ToString());
  45.  
  46. 'Then get the Item Mappings Present for the branch
  47. Dim _adpt As New gItem_BranchesTableAdapter
  48. Dim dt As New ds_CA_HO.gItem_BranchesDataTable
  49. _adpt.FillBranchMappings(dt, BranchId)
  50.  
  51. Dim _MappedItems As List(Of LocalItem) = (From _item As LocalItem In _AllItems Join _
  52. dr As ds_CA_HO.gItem_BranchesRow In dt _
  53. On _item.Id Equals dr.numItemID _
  54. Select _item).ToList
  55.  
  56. _AllItems = _AllItems.Except(_MappedItems.AsEnumerable).ToList
  57.  
  58.  
  59. Return _AllItems
  60.  
  61. var linked =
  62. from x in dc.X
  63. from y in dc.Y
  64. where x.MyProperty == y.MyProperty
  65. select x;
  66. var notLinked =
  67. dc.X.Except(linked);
  68.  
  69. class Program
  70. {
  71. static void Main(string[] args)
  72. {
  73. // creates some foos
  74. List<Foo> fooList = new List<Foo>();
  75. fooList.Add(new Foo { Id = 1, BarId = 11 });
  76. fooList.Add(new Foo { Id = 2, BarId = 12 });
  77. fooList.Add(new Foo { Id = 3, BarId = 13 });
  78. fooList.Add(new Foo { Id = 4, BarId = 14 });
  79. fooList.Add(new Foo { Id = 5, BarId = -1 });
  80. fooList.Add(new Foo { Id = 6, BarId = -1 });
  81. fooList.Add(new Foo { Id = 7, BarId = -1 });
  82.  
  83. // create some bars
  84. List<Bar> barList = new List<Bar>();
  85. barList.Add(new Bar { Id = 11 });
  86. barList.Add(new Bar { Id = 12 });
  87. barList.Add(new Bar { Id = 13 });
  88. barList.Add(new Bar { Id = 14 });
  89. barList.Add(new Bar { Id = 15 });
  90. barList.Add(new Bar { Id = 16 });
  91. barList.Add(new Bar { Id = 17 });
  92.  
  93. var linked = from foo in fooList
  94. from bar in barList
  95. where foo.BarId == bar.Id
  96. select foo;
  97. var notLinked = fooList.Except(linked);
  98. foreach (Foo item in notLinked)
  99. {
  100. Console.WriteLine(
  101. String.Format(
  102. "Foo.Id: {0} | Bar.Id: {1}",
  103. item.Id, item.BarId));
  104. }
  105. Console.WriteLine("Any key to continue...");
  106. Console.ReadKey();
  107. }
  108. }
  109.  
  110. class Foo
  111. {
  112. public int Id { get; set; }
  113. public int BarId { get; set; }
  114. }
  115.  
  116. class Bar
  117. {
  118. public int Id { get; set; }
  119. }
  120.  
  121. var items = FileMaintenanceBusiness.Instance.GetAllCodes();
  122. var result = (from i in items select i.Code).Max();
  123. List<int> countLIst = new List<int>();
  124. for (int i = 1; i <= result; i++)
  125. {
  126. countLIst.Add(i);
  127. }
  128. var query =
  129. from c in countLIst
  130. where !(from o in items
  131. select o.Code)
  132. .Contains(c)
  133. select c;
  134.  
  135.  
  136. int minID= (from q in query select q).Min();
  137. ID = minID;
Add Comment
Please, Sign In to add comment