2013-12-09 150 views
0

如何返回MULTIPLE結果匹配?返回多個匹配結果

我的方法..:

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

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

widht如何選擇數組並返回該???

+0

它是LINQ2SQL,linq2objects,linq2entities? – YD1m

+0

它是實體! ado connect – lails

回答

1

嘗試:

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

+0

...是正確的答案:-) –

+0

是的它的工作!謝謝!))) – lails

1

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); 
+0

噢...這個代碼非常困難和最長。不適合我的技能 – lails

相關問題