Home > Backend Development > C++ > How Can I Use Lambda/LINQ to Search for Multiple Keywords in a List of Comments?

How Can I Use Lambda/LINQ to Search for Multiple Keywords in a List of Comments?

Barbara Streisand
Release: 2025-01-21 12:13:09
Original
832 people have browsed it

How Can I Use Lambda/LINQ to Search for Multiple Keywords in a List of Comments?

Multi-keyword search using Lambda/LINQ with conditions

This guide demonstrates how to search for multiple keywords in the comments list at the same time. While there are many examples of searching for a single keyword, this guide provides a solution for searching for any keyword instance in one go.

To achieve this goal, you can take the following steps:

  1. Create extension method:
<code class="language-csharp">public static class QueryableExtensions
{
    public static IQueryable<T> FilterByItems<T, TItem>(this IQueryable<T> query, IEnumerable<TItem> items,
        Expression<Func<T, TItem, bool>> filterPattern, bool isOr)
    {
        // ... 方法实现 ...
    }
}</code>
Copy after login
  1. Usage:
<code class="language-csharp">var newList = MainList
    .FilterByItems(keywords, (m, k) => m.Comments.Contains(k), true)
    .ToList();</code>
Copy after login

Here, the FilterByItems method generates a predicate that performs the required filtering. The keywords parameter represents a list of search terms, and the provided lambda expression checks whether the Comments field contains any of these keywords. The isOr parameter determines whether the filtering is a logical OR (any keyword matches) or a logical AND (all keywords match).

  1. Extension method implementation:
The

FilterByItems method builds a predicate by looping over the keywords. It replaces the arguments in the provided lambda expression with each keyword, creating a separate condition for each keyword. Finally, it combines these conditions using OR or AND based on the isOr value, creating a single predicate that can be applied to the query.

  1. Auxiliary class for expression replacement:
The

ExpressionReplacer class is used to replace expressions in lambda expressions. It takes a dictionary of expressions as input and modifies the original expressions, replacing them with expressions that match the dictionary. This allows dynamic modification of predicates based on supplied keywords.

With this approach, you can use Lambda/LINQ syntax to search for multiple keywords in comments effectively and efficiently.

The above is the detailed content of How Can I Use Lambda/LINQ to Search for Multiple Keywords in a List of Comments?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template