4

Consider the following codes :

public static IQueryable<T> WhereDynamic<T>(this IQueryable<T> sourceList, string query)
{
    if (string.IsNullOrEmpty(query))
    {
        return sourceList;
    }

    try
    {
        var properties = typeof(T).GetProperties()
            .Where(x => x.CanRead && x.CanWrite && !x.GetGetMethod().IsVirtual);

        //Expression
        sourceList = sourceList.Where(c =>
            properties.Any(p => p.GetValue(c) != null && p.GetValue(c).ToString()
                .Contains(query, StringComparison.InvariantCultureIgnoreCase)));
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }

    return sourceList;
}

I have created a project of type .Net Standard 2.0 and I want to use the above code in it. But the problem is that it is not possible to use this overload:

.Contains method (query, StringComparison.InvariantCultureIgnoreCase)

It does not exist. While in a .NET Core project, there is no problem. Do you have a solution or alternative to that overload of the Contains() method?

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • According to https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=netcore-3.1#System_String_Contains_System_String_System_StringComparison_ it should be fine. Testing it myself now. It would be helpful if you could provide a [mcve] here - almost all the code here is irrelevant, as you just need to show the `string.Contains` call. – Jon Skeet Aug 12 '20 at 06:44
  • Ah, I'd misread the doc - it doesn't give per-overload version information. – Jon Skeet Aug 12 '20 at 06:46
  • [Use .NET Standard 2.1](https://stackoverflow.com/questions/444798/case-insensitive-containsstring#comment107990576_52791476) if you can. – 41686d6564 stands w. Palestine Aug 12 '20 at 06:50
  • @JonSkeet : I get this error when using a .NetStandard project: Error CS1501 No overload for method 'Contains' takes 2 arguments – farshid azizi Aug 12 '20 at 06:51
  • @JonSkeet if you go in the table of contents in the top left, and choose netstd2.0 you get the overload info for just that version https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=netstandard-2.0#System_String_Contains_System_String_System_StringComparison_ – Caius Jard Aug 12 '20 at 06:51
  • 1
    @CaiusJard: Thanks - I was just relying on the version number at the bottom, which presumably is just about "versions where any of those overloads is present". – Jon Skeet Aug 12 '20 at 07:38
  • @farshidazizi: Please put the error message in the question, with as simple an example as possible - you could write a complete class in about 8 lines of code, which would be significantly easier to understand than the code in your question. (The use of reflection and LINQ is irrelevant to your question, but it takes time to figure that out.) – Jon Skeet Aug 12 '20 at 07:40

2 Answers2

8

You can use IndexOf with a StringComparison, and then check if the result is non-negative:

string text = "BAR";
bool result = text.IndexOf("ba", StringComparison.InvariantCultureIgnoreCase) >= 0;

It's possible that there will be some really niche corner cases (e.g. with a zero-width non-joiner character) where they'll give a different result, but I'd expect them to be equivalent in almost all cases. Having said that, the .NET Core code on GitHub suggests that Contains is implemented in exactly this way anyway.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Wow is the documentation unclear about where [this overload](https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=netcore-3.1#System_String_Contains_System_String_System_StringComparison_) is available and where it isn't. Looking at that it sure seems like it should be available everywhere. – Todd Menier Sep 26 '20 at 16:49
  • @ToddMenier: I agree. The "Applies to" bit appears to mean "for any overload" rather than it being on a per-overload basis :( – Jon Skeet Sep 27 '20 at 06:08
1

Jon has the right answer, I just need to verify his answer, and Contains implementation uses IndexOf in .NET Framework. What you can do is to add extension to whatever method that is not included in .NET Standard.

for your Contains the extension would like :

public static bool Contains(this string str, string value,  StringComparison comparison)
{
    return str.IndexOf(value, comparison) >= 0;
}

You can do the same for the reset. If you need more implementations details, you could checkout Microsoft Reference which would give you a good understanding on the .NET underlying implementation.

iSR5
  • 3,274
  • 2
  • 14
  • 13