通用实体框架核心中的条件

时间:2017-03-31 08:49:44

标签: c# entity-framework entity-framework-core generic-collections

我正在构建一个使用工作单元设计模式的Web API应用程序。我创建了一个带搜索功能的存储库:

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;
}

使用枚举:

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

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

搜索功能现在运行正常。但我想减少我的代码。我想知道是否有办法创建这样的函数:

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

    return records;
}

然后我可以使用这样的功能:

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

正如您在上面看到我的代码一样,我在TextSearchMode枚举中列出了一些文本搜索模式,基于模式,我将使用Equal,EqualIgnoreCase,Contains,...搜索属性...

我正在考虑通用功能,但我现在还不知道如何创建通用功能。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

您需要传递一个定义如何从任何用户获取所需属性的Func,而不是传递user.Email。另外,传递TextSearchMode而不是传递TextSearch对象,因为您还需要Text属性来执行搜索。这是一个代码示例:

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;
}

然后像这样使用它:

users = SearchPropertyText(users, user => user.Email, emailSearch);