Iqueryable where extension method with or

Duplicate:

How to dynamically add an OR operator to a WHERE clause in LINQ

I want to iterate over an array of string values ​​and build a linq expression

If each item in the OR'ed list together.

string[] search = new string[]{"A", "B", "C"};
foreach (string item in filterValues)
{
    searchQuery = searchQuery.Where(s => s.Name.Contains(item));
}

In the above code, "A" AND "B" AND "C" were found

I want to find "A" OR "B" OR "C".

I know how to do this with Linq, but I want to do the same using extension methods.

+3
source share
5 answers

I'm late to the party, but ...

IQueryable, :

string[] search = new string[]{"A", "B", "C"};
var searchQuery = context.Users.Search(u => u.Name, search); 

: START

, , API. ,

var searchQuery = context.Users.Search(u => u.Name)
                               .Containing("A", "B", "C"); 

api : http://jnye.co/Posts/2030/searchextensions-search-strings-with-the-new-fluent-api

: END

http://jnye.co/Posts/8/generic-iqueryable-or-search-for-multiple-search-terms-using-expression-trees

https://github.com/ninjanye/SearchExtensions

nuget, :

http://www.nuget.org/packages/NinjaNye.SearchExtensions/

, , : -,

public static class QueryableExtensions  
{  
    public static IQueryable<T> Search<T>(this IQueryable<T> source, Expression<Func<T, string>> stringProperty, params string[] searchTerms)  
    {  
        if (!searchTerms.Any())  
        {  
            return source;  
        }  

        Expression orExpression = null;  
        foreach (var searchTerm in searchTerms)  
        {  
            //Create expression to represent x.[property].Contains(searchTerm)  
            var searchTermExpression = Expression.Constant(searchTerm);  
            var containsExpression = BuildContainsExpression(stringProperty, searchTermExpression);  

            orExpression = BuildOrExpression(orExpression, containsExpression);  
        }  

        var completeExpression = Expression.Lambda<Func<T, bool>>(orExpression, stringProperty.Parameters);  
        return source.Where(completeExpression);  
    }  

    private static Expression BuildOrExpression(Expression existingExpression, Expression expressionToAdd)  
    {  
        if (existingExpression == null)  
        {  
            return expressionToAdd;  
        }  

        //Build 'OR' expression for each property  
        return Expression.OrElse(existingExpression, expressionToAdd);  
    }  
}

:

string[] search = new string[]{"A", "B", "C"};
var searchQuery = context.Users.Search(u => u.Name, search);  

.

, - .

+2
var filterValues = new[] { "A", "B", "C" };

var values =
    (from item in filterValues
     from value in searchQuery
     where value.Name.Contains(item)
     select value)
     .Distinct();
+1

. , , , , . queryextender (.NET 4) . ... . , .

    Protected Sub FilterTestType(ByVal sender As Object, ByVal e As CustomExpressionEventArgs)
    Dim _codes As List(Of String) = GetTestCodes()
    e.Query = From _tests In e.Query.Cast(Of LabTest)()
              Join _code In _codes On _tests.TestCode Equals _code
              Order By _tests.LoadDate Descending
              Select _tests
End Sub
+1

:

string[] search = new string[]{"A", "B", "C"};
foreach (string item in filterValues)
{
    searchQuery = searchQuery.Where(s => s.Name.Contains(item)).Concat(searchQuery);
}

MVC3 WebApp

+1

One approach for cases where you have a known number of choices, simply compile a list of those you want to exclude, then apply the where method.

dim BASEQUERY = FROM blah in blahblahblah _
WHERE ....
select blah

For Each item In EXCLUDELIST
Dim sitem As String = item
BASEQUERY = BASEQUERY.Where(Function(py) py.YOURCOLUMN <> sitem)
Next

This will leave only the ones you want, which is equivalent to the "or where" condition. Note. A large number of excluded items can be unreasonably slow.

0
source

Source: https://habr.com/ru/post/1708458/


All Articles