lails
lails

Reputation: 105

Return multiple results matching

How return MULTIPLE results matching??

my method.. :

    public IQueryable<Products> GetSelectProduct()
    {
        int[] value = GetProdutctsBascket();

        return mm.getProducts().Where(p => p.Id.Equals(value));
    }

how select widht array and return this ???

Upvotes: 0

Views: 61

Answers (2)

jim tollan
jim tollan

Reputation: 22485

YD1m answered the question in exactly the correct way. However, I offer a small alternative - just because you can. You can use an extension method on you IEnumerable which serves the same purpose:

public static class MiscServiceTools
{
    public static IEnumerable<T> WhereIn<T, TValue>(
        this IQueryable<T> query,
        Expression<Func<T, TValue>> selector,
        params TValue[] collection) where T : class
    {
        if (selector == null) throw new ArgumentNullException("selector");
        if (collection == null) throw new ArgumentNullException("collection");
        ParameterExpression p = selector.Parameters.Single();

        if (!collection.Any()) return query;

        IEnumerable<Expression> equals = collection.Select(value =>
            (Expression) Expression.Equal(selector.Body,
                Expression.Constant(value, typeof (TValue))));

        Expression body = equals.Aggregate(Expression.Or);
        return query.Where(Expression.Lambda<Func<T, bool>>(body, p));
    }
}

the usage would then be:

return mm.getProducts().WhereIn(p => p.Id, value);

Upvotes: 1

YD1m
YD1m

Reputation: 5895

Try:

return mm.getProducts().Where(p => value.Contains(p.Id));

Upvotes: 1

Related Questions