1 public static IQueryable<TSource> WhereIn<TSource, TKey>(this IQueryable<TSource> source1, Expression<Func<TSource, TKey>> keySelector, IEnumerable<TKey> source2)
2 {
3 if (null == source1)
4 throw new ArgumentNullException("source1");
5 if (null == keySelector)
6 throw new ArgumentNullException("keySelector");
7 if (null == source2)
8 throw new ArgumentNullException("source2"); Expression where = null;
9 foreach (TKey value in source2)
10 {
11 Expression equal = Expression.Equal(keySelector.Body, Expression.Constant(value, typeof(TKey)));
12 if (null == where)
13 where = equal;
14 else
15 where = Expression.OrElse(where, equal);
16 }
17 return source1.Where<TSource>(Expression.Lambda<Func<TSource, bool>>(where, keySelector.Parameters));
18 }
上面这段是linq to sql 扩展 where in 语句,但是现在只能传一组条件如何改造可以传多组条件
当条件多时,效率很低。
它转换成了 (xx=?)or (xx=?)or (xx=?)or (xx=?)... 这种形式
能不能转换成 where in (?,?,...) 这种形式