Implement “not in” (aka “not exists”) logic in LINQ

北战南征 提交于 2019-12-02 21:51:51

问题


Setup

  1. I have two List<T>'s.
  2. The data is un-normalized and from different sources which explains the convolution in the desired logic
  3. An informal compound key in the data is fieldA, fieldB, fieldC.
  4. The "fields" are strings - reference types - so their values could be null. I want to drop records where they may be matching on null. I get that null references in C# will match, but in SQL they do not. Adding a !string.IsNullOrEmpty() is easy enough.
  5. This is not a question about DB design or relational algebra.
  6. I have other logic which covers other criteria. Do not suggest reducing the logic shown such that it might broaden the result set. See # 5 above.

The Problem

I want to find the records in listA that are not in listB based on the informal key. I then want to further refine the listA results based on a partial key match.

The SQL version of the problem:

select 
    listA.fieldA, listA.fieldB, matching.fieldC  
 from listA
 left join listB keyList on 
      listA.fieldA = keyList.fieldA and
      listA.fieldB = keyList.fieldB and
      listA.fieldC = keyList.fieldC
 inner join listB matching on
      listA.fieldA = matching.fieldA and
      listA.fieldB = matching.fieldB 
 where
    keyList.fieldA is null  

回答1:


SQL to LINQ ( Case 7 - Filter data by using IN and NOT IN clause)

Note: IN and NOT IN use the same function in the LINQ query, but it just use a ! (not) symbol for it. Here is the graphical representation:

You use, where <list>.Contains( <item> )

var myProducts = from p in db.Products
                 where productList.Contains(p.ProductID)
                 select p;

Or you can have a list predefined as such:

var ids = {1, 2, 3};

var query = from item in context.items
            where ids.Contains( item.id )
            select item;

For the 'NOT' case, just add the '!' operator before the 'Contains' statement.



来源:https://stackoverflow.com/questions/10885218/implement-not-in-aka-not-exists-logic-in-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!