开发者

Using Custom Extension Methods Inside Linq Query

开发者 https://www.devze.com 2023-01-18 20:18 出处:网络
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)

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开发者_如何学C my LINQ query. What can I do and how do I use it?


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.


You should be able to say

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

unless the class ExtensionMethods is on a different namespace.

0

精彩评论

暂无评论...
验证码 换一张
取 消