如何返回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