Click here to Skip to main content
11,502,821 members (44,654 online)
Click here to Skip to main content

Tagged as

LINQ : Implementing IN and NOT IN

, 22 Feb 2010 CPOL 11.8K 4
Rate this:
Please Sign up or sign in to vote.
I got tried of typing var result = from s in source where items.Contains(s) select s;and so I implemented the [...]

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?

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Reddit Reddit Post to StumbleUpon

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nizar Noorani
Software Developer (Senior) NCube, Inc.
United States United States
Nizar Noorani is an independent software consultant. He provides services in the areas of web applications and systems architecture, analysis, design and development. Although proficient in a variety of technologies, he specializes in the .NET technology. He can be reached at nizar.noorani@gmail.com.

Comments and Discussions

 
QuestionGood one Pin
AJD-salute6-Sep-14 17:29
memberAJD-salute6-Sep-14 17:29 
QuestionRe-inventing the wheel? Pin
Kubajzz22-Feb-10 12:53
memberKubajzz22-Feb-10 12:53 
AnswerRe: Re-inventing the wheel? Pin
Nizar Noorani14-Mar-12 4:04
memberNizar Noorani14-Mar-12 4:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 22 Feb 2010
Article Copyright 2010 by Nizar Noorani
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid