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 else15 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 语句,但是现在只能传一组条件如何改造可以传多组条件
2 回答
慕勒3428872
TA贡献1848条经验 获得超6个赞
当条件多时,效率很低。
它转换成了 (xx=?)or (xx=?)or (xx=?)or (xx=?)... 这种形式
能不能转换成 where in (?,?,...) 这种形式
- 2 回答
- 0 关注
- 626 浏览
添加回答
举报
0/150
提交
取消