IList - 用于过滤和排序的 LINQ

发布于 2024-08-17 19:21:18 字数 357 浏览 4 评论 0原文

我有以下测试代码来搜索通用列表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉鱼一梦 2024-08-24 19:21:18

您只需使用 Func 来指定您想要排序的属性:

DoSearch("foo", e => e.SomeProperty);

// ...

public void DoSearch<TKey>(string searchTerm, Func<MyEntity, TKey> orderBy)
{
    IList<MyEntity> entities = GetCollectionOfEntities();

    IList<MyEntity> results = entities
                              .Where(e => e.Description.Contains(searchTerm))
                              .OrderBy(orderBy)
                              .ToList();

    // etc
}

You just use a Func<TSource,TKey> to specify the property that you want to order by:

DoSearch("foo", e => e.SomeProperty);

// ...

public void DoSearch<TKey>(string searchTerm, Func<MyEntity, TKey> orderBy)
{
    IList<MyEntity> entities = GetCollectionOfEntities();

    IList<MyEntity> results = entities
                              .Where(e => e.Description.Contains(searchTerm))
                              .OrderBy(orderBy)
                              .ToList();

    // etc
}
看春风乍起 2024-08-24 19:21:18
    public void DoSearch(string searchTerm, Func<MyEntity, PropertyType> selector)
    {

       IList<MyEntity> entities = GetCollectionOfEntities();

       IList<MyEntity> results = entities
                      .Where(d => d.Description.Contains(searchTerm))
                      .OrderBy(selector)
                      .ToList();

   }

   DoSearch("searchTerm", entity => entity.Property)

PropertyType 是要排序的属性的类型。否则,您可以将其设为通用,如下所示:

    public void DoSearch<TKey>(string searchTerm, Func<MyEntity, Tkey> selector)

并调用它。

    public void DoSearch(string searchTerm, Func<MyEntity, PropertyType> selector)
    {

       IList<MyEntity> entities = GetCollectionOfEntities();

       IList<MyEntity> results = entities
                      .Where(d => d.Description.Contains(searchTerm))
                      .OrderBy(selector)
                      .ToList();

   }

   DoSearch("searchTerm", entity => entity.Property)

PropertyType is the type of the property you want to sort. Else you could make it Generic like this:

    public void DoSearch<TKey>(string searchTerm, Func<MyEntity, Tkey> selector)

And call it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文