我想通过使用我的自定义函数检查它们来获取一些元素。
我有人员表:
public class Person
{
public int Id { get; set; }
public DateTime BirthDay { get; set; }
public string Name { get; set; }
...
}
我应该使用我的GetAge()
和其他功能来过滤人员列表。我的以下代码不起作用:
public List<Person> FilterPersons(int ageFrom, int ageTo...etc..)
{
var all = Database.Persons.AsQueryable();
all = from item in all
where GetAge(item.BirthDay) > ageFrom
select item;
all = from item in all
where GetAge(item.BirthDay) < ageTo
select item;
// other operations
...
}
我想我可以这样写。在执行此操作的每个步骤中:
List<Person> newList = new List<Person>();
foreach (var item in all)
{
var itemAge = Common.GetAge(item.BirthDay);
if (itemAge > AgeFrom)
{
newList.Add(item);
}
}
all = newList.List();
但这不是我认为的最佳方式,因为我应该按许多标准进行过滤。它将以低速工作。
如何在 Linq 查询中使用我的函数?
编辑: 例如,我展示了 GetAge() 函数。我有很多这样的功能。我想知道如何使用我的功能。