Generic where condition in Entity Framework Core

c# entity-framework entity-framework-core generic-collections

Question

I'm building a Web API application which use Unit of work design pattern. I've created a repository with search function:

public IQueryable<User> Search(SearchUserViewModel parameter)
{
    // Find all user from database
    var users = GetAll();

    // Id has been defined.
    if (parameter.Id != null)
        users = users.Where(x => x.Id == parameter.Id.Value);

    // Email has been defined.
    if (parameter.Email != null && !string.IsNullOrWhiteSpace(parameter.Email.Text))
    {
        var emailSearch = parameter.Email;
        switch (emailSearch.Mode)
        {
            case TextSearchMode.ContainIgnoreCase:
                // TODO: Recheck.
                users = users.Where(x => x.Email.Contains(emailSearch.Text));
                break;
            case TextSearchMode.Equal:
                users = users.Where(x => x.Email.Equals(emailSearch.Text));
                break;
            case TextSearchMode.EqualIgnoreCase:
                users = users.Where(x => x.Email.Equals(emailSearch.Text, StringComparison.OrdinalIgnoreCase));
                break;
            default:
                users = users.Where(x => x.Email.Contains(emailSearch.Text));
                break;
        }
    }

    if (parameter.Password != null && !string.IsNullOrWhiteSpace(parameter.Password.Text))
    {
        var passwordSearch = parameter.Password;
        switch (passwordSearch.Mode)
        {
            case TextSearchMode.ContainIgnoreCase:
                // TODO: Recheck.
                users = users.Where(x => x.Email.Contains(passwordSearch.Text));
                break;
            case TextSearchMode.Equal:
                users = users.Where(x => x.Email.Equals(passwordSearch.Text));
                break;
            case TextSearchMode.EqualIgnoreCase:
                users = users.Where(x => x.Email.Equals(passwordSearch.Text, StringComparison.OrdinalIgnoreCase));
                break;
            default:
                users = users.Where(x => x.Email.Contains(passwordSearch.Text));
                break;
        }
    }

    return users;
}

With an enumeration:

public class TextSearch
{
    /// <summary>
    /// Comparision mode.
    /// </summary>
    public TextSearchMode Mode { get; set; }

    /// <summary>
    /// Text which needs searching.
    /// </summary>
    public string Text { get; set; }
}

Search function runs fine for now. But I want to reduce my code. I wonder if there is a way to create a function like this one:

private IQueryable<T> SearchPropertyText(IQueryable<T> records, string propertyName, TextSearchMode mode)
{
    switch (mode)
    {
        case Equal:
            // TODO:
        case EqualIgnoreCase:
            // TODO:
        default:
            // TODO
    }

    return records;
}

Then I can use the function like this:

users = SearchPropertyText(users, user.Email, emailSearch.Mode)

As you've seen my code above, I have some mode of text searching listed in the TextSearchMode enumeration, base on mode, I'll search property with Equal, EqualIgnoreCase, Contains, ...

I'm thinking about generic function, but I don't know how to create it for now.

Can anyone help me please ?

1
0
3/31/2017 8:49:44 AM

Accepted Answer

Rather than passing user.Email, you need to pass a Func that defines how to get the desired property from any user. Also, rather that passing a TextSearchMode, you should pass a TextSearch object since you also need the Text property to perform the search. Here is a code example:

private IQueryable<T> SearchPropertyText(IQueryable<T> records, Func<T, string> property, TextSearch search)
{
    switch (search.Mode)
    {
        case TextSearchMode.ContainIgnoreCase:
            records = records.Where(x => property(x).Contains(search.Text));
            break;
        case TextSearchMode.Equal:
            records = records.Where(x => property(x).Equals(search.Text));
            break;
        case TextSearchMode.EqualIgnoreCase:
            records = records.Where(x => property(x).Equals(search.Text, StringComparison.OrdinalIgnoreCase));
            break;
        default:
            records = records.Where(x => property(x).Contains(search.Text));
            break;
    }
    return records;
}

Then use it like so:

users = SearchPropertyText(users, user => user.Email, emailSearch);
1
3/31/2017 9:33:06 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow