Home > C#, CodeProject, LINQ > LINQ : Implementing IN and NOT IN

LINQ : Implementing IN and NOT IN

February 12th, 2009 Nizar Leave a comment Go to comments

I got tried of typing

  var result = from s in source
                  where items.Contains(s)
                  select s;

and so I implemented the IN and NOT IN methods as extension methods:

public static IQueryable<T> In<T>(this IQueryable<T> source, 
                            IQueryable<T> checkAgainst)
{
   return from s in source 
            where checkAgainst.Contains(s) 
            select s;
}
 
public static IQueryable<T> NotIn<T>(this IQueryable<T> source, 
                                  IQueryable<T> checkAgainst)
{
   return from s in source 
            where !checkAgainst.Contains(s) 
            select s;
}

Thus, I can now just do the following:

  var result = source.In(items);

What do you think – Good idea, Bad idea, Useless idea?

Kick It on DotNetKicks.com Shout it
  1. Kevin Buchan
    February 20th, 2009 at 06:08 | #1

    I likes it! Beautiful example of a useful extension method.

  2. Nizar
    February 22nd, 2009 at 08:22 | #2

    Thanks!

  3. August 26th, 2009 at 01:06 | #3

    I think the idea is good but you need to modify it a little.

  4. Nizar
    August 26th, 2009 at 08:09 | #4

    Can you let me know what this modification needs to be? You can email it to me if you like: nizarnoorani@gmail.com. Thanks!

  5. Janaranjan
    February 16th, 2010 at 10:37 | #5

    Nizar,

    This is not working in Entity Framework model for some reason.

  6. Nizar
    February 17th, 2010 at 12:06 | #6

    What is not in Entity Framework model?

  7. James
    May 24th, 2010 at 11:26 | #7

    I want to be sure I understand what this is supposed to do. I’m building an app that includes searching of documents by the text they contain, but I want to exclude very common words, like pronouns, prepositions, and so forth, which we call stopwords in the business.

    So I’ve got a list of all single words — IQueryable SingleWords

    and a list of stopwords — IQueryableStopWords

    So what I need to do is return a third list equivalent to SingleWords exclusive of any Stopwords. If I code your methods in the OP, should I then be able to code this as follows below?

    RevisedList=SingleWords.NotIn(StopWords);

    If I code your In and NotIn, and I have two IQueryable sets as follows:

    IQueryable SearchTerms
    IQueryable StopWords

    I should be able to do this:

    result=SearchTerms.NotIn(StopWords)

  8. James
    May 25th, 2010 at 00:13 | #8

    I was able to get this to work, but other newbie C# programmers like me might find it helpful to know exactly where I implemented this in my project. I had set up a utility dll project within the solution to handle text parsing chores, one of which is to weed out unwanted words based on a database table, as I mentioned above. In order for it to work, I had to put Nizar’s methods in a static class that does nothing more than hold those behaviors:

    namespace TextParsingUtilities
    {
    public static class ExtendIQueryable
    {
    public static IQueryable In ( ….
    (as given above)

    public static IQueryable NotIn ( …
    (as given above)
    }

    public class StopListFunctions
    {
    //class includes several utility methods which collect a text string, read the stop
    //from the database and excludes the stopwords from the text string. One of these
    //methods includes the call to the NotIn function above.
    }
    }

    For some reason it didn’t work if I simply put Nizar’s functions into the StopListFunctions class, but by moving them to their own static class in the same namespace, it works correctly.

  1. No trackbacks yet.