Using custom extension methods inside Linq Query

I made my own 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 to use it?

0
source share
2 answers

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

If you really want to use your method, then it will work fine in a LINQ to Objects query, but it cannot be used in a database query.

+4
source

You should be able to say

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

if the ExtensionMethods class is not in a different namespace.

0
source

Source: https://habr.com/ru/post/953868/


All Articles