LINQ到SQL:结合(或运算)多与QUOT;包含"过滤器? [英] Linq-to-SQL: Combining (OR'ing) multiple "Contains" filters?

查看:131
本文介绍了LINQ到SQL:结合(或运算)多与QUOT;包含"过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦搞清楚做到这一点的最好办法,我就AP preciate任何帮助。

I'm having some trouble figuring out the best way to do this, and I would appreciate any help.

基本上,我设立一个过滤器,允许用户看的与用户名的任意过滤器相关联的审核项的历史。

Basically, I'm setting up a filter that allows the user to look at a history of audit items associated with an arbitrary "filter" of usernames.

数据源是SQL Server数据库,所以我走的是IQueryable的源(无论是从数据库上下文对象,或者是一个IQueryable直接表引用这使我们从其他查询),应用WHERE过滤器,然后返回所产生的IQueryable对象....但我有点难倒就如何执行或使用这种方法。

The datasource is a SQL Server data base, so I'm taking the IQueryable "source" (either a direct table reference from the db context object, or perhaps an IQueryable that's resulted from additional queries), applying the WHERE filter, and then returning the resultant IQueryable object....but I'm a little stumped as to how to perform OR using this approach.

我已经考虑过的前pressions的路线,因为我知道如何还是那些,但我一直没能找出相当如何做到这一点与包含式的评价,所以我米目前使用UNION,但恐怕这可能会对性能造成负面影响,我想知道,如果它不能给我正是我需要的,如果其他过滤器(除用户名的过滤如图所示)的加入一个arbirary订单。

I've considered going the route of Expressions because I know how to OR those, but I haven't been able to figure out quite how to do that with a "Contains" type evaluation, so I'm currently using a UNION, but I'm afraid this might have negative impact on performance, and I'm wondering if it may not give me exactly what I need if other filters (in addition to user name filtering shown here) are added in an arbirary order.

下面是我的示例code:

Here is my sample code:

public override IQueryable<X> ApplyFilter<X>(IQueryable<X> source)
{
    // Take allowed values...
    List<string> searchStrings = new List<string>();

    // <SNIP> (This just populates my list of search strings)

    IQueryable<X> oReturn = null;

    // Step through each iteration, and perform a 'LIKE %value%' query
    string[] searchArray = searchStrings.ToArray();
    for (int i = 0; i < searchArray.Length; i++)
    {
        string value = searchArray[i];
        if (i == 0)
            // For first step, perform direct WHERE
            oReturn = source.Where(x => x.Username.Contains(value));
        else
            // For additional steps, perform UNION on WHERE
            oReturn = oReturn.Union(source.Where(x => x.Username.Contains(value)));
    }
    return oReturn ?? source;
}

这感觉就像是错误的方式做事情,但它似乎工作,所以我的问题是第一次,有没有更好的办法做到这一点?此外,有没有办法做一个'包含'或'像'与前pressions?

This feels like the wrong way to do things, but it does seem to work, so my question is first, is there a better way to do this? Also, is there a way to do a 'Contains' or 'Like' with Expressions?

(Editted改正我的code:在回滚,以便将它张贴在工作状态,我显然没有回退到很远不够:))

(Editted to correct my code: In rolling back to working state in order to post it, I apparently didn't roll back quite far enough :) )

=============================================

=============================================

ETA:按所给的解决方案,这是我新的code(万一有人读这有兴趣):

public override IQueryable<X> ApplyFilter<X>(IQueryable<X> source)
{
    List<string> searchStrings = new List<string>(AllowedValues);

    // <SNIP> build collection of search values 

    string[] searchArray = searchStrings.ToArray();

    Expression<Func<X, bool>> expression = PredicateBuilder.False<X>();

    for (int i = 0; i < searchArray.Length; i++)
    {
        string value = searchArray[i];
        expression = expression.Or(x => x.Username.Contains(value));
    }
    return source.Where(expression);
}

(但是要注意一点,我注意到:继predicateBuilder的例子​​中,搜索字符串的一个空的集合将返回false(假||值1 || ...),而在我原来的版本,我是假设一个空​​列表应该只是coallesce到未过滤的源。当我想到了更多的,新的版本似乎更有意义,我的需要,所以我采取了)

(One caveat I noticed: Following the PredicateBuilder's example, an empty collection of search strings will return false (false || value1 || ... ), whereas in my original version, I was assuming an empty list should just coallesce to the unfiltered source. As I thought about it more, the new version seems to make more sense for my needs, so I adopted that)

=============================================

=============================================

推荐答案

您可以使用 predicateBuilder 从LINQkit动态地构造查询。

You can use the PredicateBuilder from the LINQkit to dynamically construct your query.

这篇关于LINQ到SQL:结合(或运算)多与QUOT;包含&QUOT;过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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