【发布时间】:2020-12-19 22:24:45
【问题描述】:
考虑一个类:
public class Dog
{
[Key]
[TableField(Name= "Jersey", Inoculated = false)]
public string Param1{ get; set; }
[TableField(Name= "Daisy", Inoculated = true)]
public string Param2{ get; set; }
[TableField(Name= "Jeremy", Inoculated = true)]
public string Param3{ get; set; }
}
还有一个属性类:
public sealed class TableField : Attribute
{
public string Name { get; set; }
public bool Inoculated { get; set; }
}
这与现实生活中的例子有点远,但我需要从 typeof(Dog) (默认类值)中选择所有 TableField.Name 属性值,其中 TableField.Inoculated == true。
最佳尝试,不知道从哪里开始:
var names = typeof(Dog).GetProperties()
.Where(r => r.GetCustomAttributes(typeof(TableField), false).Cast<TableField>()
.Any(x => x.Inoculated));
【问题讨论】:
-
您要的是
IEnumerable<T>还是IQueriable<T>?
标签: c# linq attributes filtering