Search code examples
c#linqdynamicdynamic-linq

System.Linq.Dynamic Error No property or field 'StringComparison' exists in type 'Person'


I am using System.Linq.Dynamic (Install-Package System.Linq.Dynamic) and I am trying to use the IndexOf overload with the StringComparison. However, it's acting like it is trying to apply the StringComparison operator to the Person object. Am I writing the query correctly?

try
{
     IEnumerable<Person> dynamicLinqItems = people.Where("(FirstName.IndexOf(@0, StringComparison.OrdinalIgnoreCase) >= 0)", "T");
}
catch (Exception exception)
{
    Console.WriteLine(exception);
}

Error Message

No property or field 'StringComparison' exists in type 'Person'

Object

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthday { get; set; }

    public int Age
    {
        get
        {
            DateTime now = DateTime.Today;
            int age = now.Year - Birthday.Year;
            if (now < Birthday.AddYears(age))
            {
                age--;
            }
            return age;
        }
    }
}

Solution

  • It may be an issue with using an enum: How to use Enums with Dynamic Linq?

    Try the following:

    IEnumerable<Person> dynamicLinqItems = people.Where("(FirstName.IndexOf(@0, @1) >= 0)", "T", StringComparison.OrdinalIgnoreCase);