如何返回MULTIPLE結果匹配?返回多個匹配結果
我的方法..:
public IQueryable<Products> GetSelectProduct()
{
int[] value = GetProdutctsBascket();
return mm.getProducts().Where(p => p.Id.Equals(value));
}
widht如何選擇數組並返回該???
如何返回MULTIPLE結果匹配?返回多個匹配結果
我的方法..:
public IQueryable<Products> GetSelectProduct()
{
int[] value = GetProdutctsBascket();
return mm.getProducts().Where(p => p.Id.Equals(value));
}
widht如何選擇數組並返回該???
嘗試:
return mm.getProducts().Where(p => value.Contains(p.Id));
...是正確的答案:-) –
是的它的工作!謝謝!))) – lails
YD1m回答完全正確的方式的問題。但是,我提供了一個小的選擇 - 只是因爲你可以。您可以使用它具備相同的目的,你的擴展方法IEnumerable:
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));
}
}
的使用將被:
return mm.getProducts().WhereIn(p => p.Id, value);
噢...這個代碼非常困難和最長。不適合我的技能 – lails
它是LINQ2SQL,linq2objects,linq2entities? – YD1m
它是實體! ado connect – lails