我正在使用 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;
}
}
}
最佳答案
使用枚举可能会出现问题:How to use Enums with Dynamic Linq?
尝试以下操作:
IEnumerable<Person> dynamicLinqItems = people.Where("(FirstName.IndexOf(@0, @1) >= 0)", "T", StringComparison.OrdinalIgnoreCase);
关于c# - System.Linq.Dynamic 错误 类型 'StringComparison' 中不存在属性或字段 'Person',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36480853/