【问题标题】:System.Linq.Dynamic Error No property or field 'StringComparison' exists in type 'Person'System.Linq.Dynamic 错误“Person”类型中不存在属性或字段“StringComparison”
【发布时间】:2016-07-28 14:39:18
【问题描述】:

我正在使用 System.Linq.Dynamic (Install-Package System.Linq.Dynamic),并且我正在尝试将 IndexOf 重载与 StringComparison 一起使用。但是,它的行为就像是在尝试将 StringComparison 运算符应用于 Person 对象。我是否正确编写查询?

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

错误信息

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

对象

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;
        }
    }
}

【问题讨论】:

    标签: c# linq dynamic dynamic-linq


    【解决方案1】:

    使用枚举可能有问题:How to use Enums with Dynamic Linq?

    尝试以下方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2017-03-26
      • 2018-01-29
      相关资源
      最近更新 更多