john doe
john doe

Reputation: 17

Using Custom Extension Methods Inside Linq Query

I have made my custom In extension method as shown below:

 public static class ExtensionMethods
    {
        public static bool In(this string str, IEnumerable<String> list)
        {
            foreach (var s in list)
            {
                if (s.Equals(str)) return true; 
            }

            return false; 
        }
    }

And now I like to use it with my LINQ query. What can I do and how do I use it?

Upvotes: 0

Views: 1132

Answers (2)

Viv
Viv

Reputation: 2595

You should be able to say

if (stringName.In(listVariableName)){....}

unless the class ExtensionMethods is on a different namespace.

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838216

I think your method is very similar to Enumerable.Contains. Perhaps you could just use that instead.

If you really want to use your method then it will work fine in a LINQ to Objects query, but it won't be possible to use it in a database query.

Upvotes: 4

Related Questions