LINQ for querying LIKE array elements

Let's say I have an array, and I want to make a LINQ query to varchar, which returns any records that have an array element anywhere in varchar.

Something like this would be sweet.

string[] industries = { "airline", "railroad" }

var query = from c in contacts where c.industry.LikeAnyElement (industries) select c

Any ideas?

+5
source share
5 answers

, Express Yourself -, LINQ; , - . ( , StartsWith ..):

    using (var ctx = new NorthwindDataContext())
    {
        ctx.Log = Console.Out;
        var data = ctx.Customers.WhereTrueForAny(
            s => cust => cust.CompanyName.Contains(s),
            "a", "de", "s").ToArray();
    }
// ...
public static class QueryableExt
{
    public static IQueryable<TSource> WhereTrueForAny<TSource, TValue>(
        this IQueryable<TSource> source,
        Func<TValue, Expression<Func<TSource, bool>>> selector,
        params TValue[] values)
    {
        return source.Where(BuildTrueForAny(selector, values));
    }
    public static Expression<Func<TSource, bool>> BuildTrueForAny<TSource, TValue>(
        Func<TValue, Expression<Func<TSource, bool>>> selector,
        params TValue[] values)
    {
        if (selector == null) throw new ArgumentNullException("selector");
        if (values == null) throw new ArgumentNullException("values");
        if (values.Length == 0) return x => true;
        if (values.Length == 1) return selector(values[0]);

        var param = Expression.Parameter(typeof(TSource), "x");
        Expression body = Expression.Invoke(selector(values[0]), param);
        for (int i = 1; i < values.Length; i++)
        {
            body = Expression.OrElse(body,
                Expression.Invoke(selector(values[i]), param));
        }
        return Expression.Lambda<Func<TSource, bool>>(body, param);
    }

}
+5
from c in contracts 
where industries.Any(i => i == c.industry)
select c;

- . .

+4

IEnumerable.Contains() SQL IN, :

WHERE 'american airlines' IN ('airline', 'railroad') -- FALSE

String.Contains(), SQL LIKE%...%, :

WHERE 'american airlines' LIKE '%airline%' -- TRUE

, , LIKE () , Any(), String.Contains() :

string[] industries = { "airline", "railroad" };

var query = from c in contacts 
            where industries.Any(i => c.Industry.Contains(i))
            select c;

, Any(), String.Contains(), , LINQ to SQL. , - :

where c.Industry.Contains("airline") ||
      c.Industry.Contains("railroad") || ...

( ), , LINQ , .AsEnumerable() contacts.ToList() :

var query = from c in contacts.AsEnumerable()
            where industries.Any(i => c.Industry.Contains(i))
            select c;
+3

, :

var query = from c . AsEnumerable()            c;

query = query.Where(c = > (c.Industry.Contains( "" )) || (c.Industry.Contains( "railroad" )));

, . , . . - http://www.albahari.com/nutshell/predicatebuilder.aspx

+1

, LIKE LINQ to SQL, :

http://msdn.microsoft.com/en-us/library/bb882677.aspx

, , , , LINQ to SQL.


, Contains. , , . , , , , :

industry LIKE '%<element>%'

, IndexOf ( SQL):

string[] industries = { "airline", "railroad" }

var query = 
    from c in contacts 
    where
        c.industry.IndexOf(industries[0]) != -1 ||
        c.industry.IndexOf(industries[1]) != -1

If you know the length of the array and the number of elements, you can do this. If you do not, you will need to create an Expression instance based on the array and field you are looking for.

0
source

All Articles