ASP.NET搜索表单 - 动态LINQ to SQL的? [英] ASP.NET search form - dynamic Linq to SQL?

查看:79
本文介绍了ASP.NET搜索表单 - 动态LINQ to SQL的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索表单,允许用户在多种不同的方式几种不同的字段进行搜索。这里是我的code的例子。

I have a search form that allows users to search on several different fields in several different ways. Here is an example of my code.

var claims = from c in db.Claims select c;

switch (ddlSearchField.Text)
{
    case "StartsWith":
        claims = claims.Where(c => c.companyFileID.StartsWith(txtSearchBox.Text));
        break;

    case "Equals":
        claims = claims.Where(c => c.companyFileID == txtSearchBox.Text);
        break;

    case "Contains":
        claims = claims.Where(c => c.companyFileID.Contains(txtSearchBox.Text));
        break;
}

我有十个不同的领域,用户可以搜索,所以我外面的开关语句是pretty大。必须有做到这一点更优雅的方式。

I have about ten different fields that a user can search on so my outer switch statement is pretty large. There has to be a more elegant way to accomplish this.

推荐答案

您可以通过创建一个扩展方法重构code的某些部分。这样的事情:

You could refactor some parts of the code by creating an extension method. Something like that :

static class QueryableExtensions
{
    private static MethodInfo StringContainsMethod;
    private static MethodInfo StringStartsWithMethod;

    static QueryableExtensions()
    {
        Type[] singleStringParam = new[] {typeof(string)};
        StringContainsMethod = typeof(string).GetMethod("Contains", singleStringParam);
        StringStartsWithMethod = typeof(string).GetMethod("StartsWith", singleStringParam);
    }

    public static IQueryable<T> AppendTextFilter<T>(this IQueryable<T> queryable, Expression<Func<T, string>> memberSelector, string condition, string value)
    {
        Expression expression = null;
        switch (condition)
        {
            case "StartsWith":
                expression = Expression.Call(
                                memberSelector.Body,
                                StringStartsWithMethod,
                                Expression.Constant(value));
                break;

            case "Equals":
                expression = Expression.Equal(
                                memberSelector.Body,
                                Expression.Constant(value));
                break;

            case "Contains":
                expression = Expression.Call(
                                memberSelector.Body,
                                StringContainsMethod,
                                Expression.Constant(value));
                break;

            default:
                throw new NotSupportedException(string.Format("'{0}' is not a supported condition", condition));
        }

        var lambda = Expression.Lambda<Func<T, bool>>(
                        expression,
                        memberSelector.Parameters);
        return queryable.Where(lambda);
    }
}

然后,您可以使用它像:

You could then use it like that :

var claims = db.Claims
             .AppendTextFilter(c => c.companyFileID, ddlSearchField.Text, txtSearchBox.Text)
             .AppendTextFilter(c => c.someOtherProperty, ddlOtherSearchField.Text, txtOtherSearchBox.Text)
             ...;

这篇关于ASP.NET搜索表单 - 动态LINQ to SQL的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆