IList - 用于过滤和排序的 LINQ
我有以下测试代码来搜索通用列表:
public void DoSearch(string searchTerm)
{
IList<MyEntity> entities = GetCollectionOfEntities();
IList<MyEntity> results = entities.Where(d => d.Description.Contains(searchTerm)).ToList();
}
我想按参数传递订单(这将是 MyEntity 的属性),当然还可以根据该参数对结果进行排序。我了解 LINQ 使用 OrderBy 但不了解如何按 MyEntity 的属性进行排序。
I have the following test code to search a generic list:
public void DoSearch(string searchTerm)
{
IList<MyEntity> entities = GetCollectionOfEntities();
IList<MyEntity> results = entities.Where(d => d.Description.Contains(searchTerm)).ToList();
}
I want to pass an order by parameter (which would be a property of MyEntity) and of course order my results based on that. I understand LINQ uses OrderBy but do not understand how to order by a property of MyEntity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需使用
Func
来指定您想要排序的属性:You just use a
Func<TSource,TKey>
to specify the property that you want to order by:PropertyType 是要排序的属性的类型。否则,您可以将其设为通用,如下所示:
并调用它。
PropertyType is the type of the property you want to sort. Else you could make it Generic like this:
And call it.